summaryrefslogtreecommitdiff
path: root/src/backends/native/meta-udev.c
blob: 5d58d6b56f741d6634d0588bbf63190c90ad73e0 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright (C) 2018 Red Hat
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 */

#include "config.h"

#include "backends/native/meta-udev.h"

#include "backends/native/meta-backend-native.h"
#include "backends/native/meta-launcher.h"

#define DRM_CARD_UDEV_DEVICE_TYPE "drm_minor"

enum
{
  HOTPLUG,
  DEVICE_ADDED,
  DEVICE_REMOVED,

  N_SIGNALS
};

static guint signals[N_SIGNALS];

struct _MetaUdev
{
  GObject parent;

  MetaBackendNative *backend_native;

  GUdevClient *gudev_client;

  gulong uevent_handler_id;
};

G_DEFINE_TYPE (MetaUdev, meta_udev, G_TYPE_OBJECT)

gboolean
meta_is_udev_device_platform_device (GUdevDevice *device)
{
  g_autoptr (GUdevDevice) platform_device = NULL;

  platform_device = g_udev_device_get_parent_with_subsystem (device,
                                                             "platform",
                                                             NULL);
  return !!platform_device;
}

gboolean
meta_is_udev_device_boot_vga (GUdevDevice *device)
{
  g_autoptr (GUdevDevice) pci_device = NULL;

  pci_device = g_udev_device_get_parent_with_subsystem (device, "pci", NULL);
  if (!pci_device)
    return FALSE;

  return g_udev_device_get_sysfs_attr_as_int (pci_device, "boot_vga") == 1;
}

gboolean
meta_is_udev_device_disable_modifiers (GUdevDevice *device)
{
  g_autoptr (GUdevDevice) platform_device = NULL;
  const char * const * tags;

  platform_device = g_udev_device_get_parent_with_subsystem (device,
                                                             "platform",
                                                             NULL);

  if (!platform_device)
    return FALSE;

  tags = g_udev_device_get_tags (platform_device);

  if (!tags)
    return FALSE;

  return g_strv_contains (tags, "mutter-device-disable-kms-modifiers");
}

gboolean
meta_is_udev_device_preferred_primary (GUdevDevice *device)
{
  const char * const * tags;

  tags = g_udev_device_get_tags (device);
  if (!tags)
    return FALSE;

  return g_strv_contains (tags, "mutter-device-preferred-primary");
}

gboolean
meta_udev_is_drm_device (MetaUdev    *udev,
                         GUdevDevice *device)
{
  const char *seat_id;
  const char *device_type;
  const char *device_seat;

  /* Filter out devices that are not character device, like card0-VGA-1. */
  if (g_udev_device_get_device_type (device) != G_UDEV_DEVICE_TYPE_CHAR)
    return FALSE;

  device_type = g_udev_device_get_property (device, "DEVTYPE");
  if (g_strcmp0 (device_type, DRM_CARD_UDEV_DEVICE_TYPE) != 0)
    return FALSE;

  device_seat = g_udev_device_get_property (device, "ID_SEAT");
  if (!device_seat)
    {
      /* When ID_SEAT is not set, it means seat0. */
      device_seat = "seat0";
    }

  /* Skip devices that do not belong to our seat. */
  seat_id = meta_backend_native_get_seat_id (udev->backend_native);
  if (g_strcmp0 (seat_id, device_seat))
    return FALSE;

  return TRUE;
}

GList *
meta_udev_list_drm_devices (MetaUdev  *udev,
                            GError   **error)
{
  g_autoptr (GUdevEnumerator) enumerator = NULL;
  GList *devices;
  GList *l;

  enumerator = g_udev_enumerator_new (udev->gudev_client);

  g_udev_enumerator_add_match_name (enumerator, "card*");
  g_udev_enumerator_add_match_tag (enumerator, "seat");

  /*
   * We need to explicitly match the subsystem for now.
   * https://bugzilla.gnome.org/show_bug.cgi?id=773224
   */
  g_udev_enumerator_add_match_subsystem (enumerator, "drm");

  devices = g_udev_enumerator_execute (enumerator);
  if (!devices)
    return NULL;

  for (l = devices; l;)
    {
      GUdevDevice *device = l->data;
      GList *l_next = l->next;

      if (!meta_udev_is_drm_device (udev, device))
        {
          g_object_unref (device);
          devices = g_list_delete_link (devices, l);
        }

      l = l_next;
    }

  return devices;
}

static void
on_uevent (GUdevClient *client,
           const char  *action,
           GUdevDevice *device,
           gpointer     user_data)
{
  MetaUdev *udev = META_UDEV (user_data);

  if (!g_udev_device_get_device_file (device))
    return;

  if (g_str_equal (action, "add"))
    g_signal_emit (udev, signals[DEVICE_ADDED], 0, device);
  else if (g_str_equal (action, "remove"))
    g_signal_emit (udev, signals[DEVICE_REMOVED], 0, device);

  if (g_udev_device_get_property_as_boolean (device, "HOTPLUG"))
    g_signal_emit (udev, signals[HOTPLUG], 0);
}

MetaUdev *
meta_udev_new (MetaBackendNative *backend_native)
{
  MetaUdev *udev;

  udev = g_object_new (META_TYPE_UDEV, NULL);
  udev->backend_native = backend_native;

  return udev;
}

static void
meta_udev_finalize (GObject *object)
{
  MetaUdev *udev = META_UDEV (object);

  g_clear_signal_handler (&udev->uevent_handler_id, udev->gudev_client);
  g_clear_object (&udev->gudev_client);

  G_OBJECT_CLASS (meta_udev_parent_class)->finalize (object);
}

static void
meta_udev_init (MetaUdev *udev)
{
  const char *subsystems[] = { "drm", NULL };

  udev->gudev_client = g_udev_client_new (subsystems);
  udev->uevent_handler_id = g_signal_connect (udev->gudev_client,
                                              "uevent",
                                              G_CALLBACK (on_uevent), udev);
}

static void
meta_udev_class_init (MetaUdevClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = meta_udev_finalize;

  signals[HOTPLUG] =
    g_signal_new ("hotplug",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
                  0, NULL, NULL, NULL,
                  G_TYPE_NONE, 0);
  signals[DEVICE_ADDED] =
    g_signal_new ("device-added",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
                  0, NULL, NULL, NULL,
                  G_TYPE_NONE, 1,
                  G_UDEV_TYPE_DEVICE);
  signals[DEVICE_REMOVED] =
    g_signal_new ("device-removed",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
                  0, NULL, NULL, NULL,
                  G_TYPE_NONE, 1,
                  G_UDEV_TYPE_DEVICE);
}