1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
10 #include "alloc-util.h"
11 #include "logind-device.h"
14 Device* device_new(Manager *m, const char *sysfs, bool master) {
24 d->sysfs = strdup(sysfs);
28 if (hashmap_put(m->devices, d->sysfs, d) < 0) {
35 dual_timestamp_get(&d->timestamp);
40 static void device_detach(Device *d) {
49 while ((sd = d->session_devices))
50 session_device_free(sd);
53 LIST_REMOVE(devices, d->seat->devices, d);
56 if (!seat_has_master_device(s)) {
57 seat_add_to_gc_queue(s);
58 seat_send_changed(s, "CanGraphical", NULL);
62 void device_free(Device *d) {
67 hashmap_remove(d->manager->devices, d->sysfs);
73 void device_attach(Device *d, Seat *s) {
87 had_master = seat_has_master_device(s);
89 /* We keep the device list sorted by the "master" flag. That is, master
90 * devices are at the front, other devices at the tail. As there is no
91 * way to easily add devices at the list-tail, we need to iterate the
92 * list to find the first non-master device when adding non-master
93 * devices. We assume there is only a few (normally 1) master devices
94 * per seat, so we iterate only a few times. */
96 if (d->master || !s->devices)
97 LIST_PREPEND(devices, s->devices, d);
99 LIST_FOREACH(devices, i, s->devices) {
100 if (!i->devices_next || !i->master) {
101 LIST_INSERT_AFTER(devices, s->devices, i, d);
107 if (!had_master && d->master)
108 seat_send_changed(s, "CanGraphical", NULL);