]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/logind-device.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / login / logind-device.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
20263082 2
20263082
LP
3#include <string.h>
4
b5efdb8a 5#include "alloc-util.h"
cc377381 6#include "logind-device.h"
6ecda0fb 7#include "logind-seat-dbus.h"
b5efdb8a 8#include "util.h"
20263082 9
718d006a 10Device* device_new(Manager *m, const char *sysfs, bool master) {
20263082
LP
11 Device *d;
12
13 assert(m);
14 assert(sysfs);
15
16 d = new0(Device, 1);
17 if (!d)
18 return NULL;
19
20 d->sysfs = strdup(sysfs);
6b430fdb
ZJS
21 if (!d->sysfs)
22 return mfree(d);
20263082
LP
23
24 if (hashmap_put(m->devices, d->sysfs, d) < 0) {
25 free(d->sysfs);
6b430fdb 26 return mfree(d);
20263082
LP
27 }
28
29 d->manager = m;
718d006a 30 d->master = master;
20263082
LP
31 dual_timestamp_get(&d->timestamp);
32
33 return d;
34}
35
9588bc32 36static void device_detach(Device *d) {
f1a8e221 37 Seat *s;
118ecf32 38 SessionDevice *sd;
cc377381 39
20263082
LP
40 assert(d);
41
f1a8e221
LP
42 if (!d->seat)
43 return;
20263082 44
118ecf32
DH
45 while ((sd = d->session_devices))
46 session_device_free(sd);
47
f1a8e221 48 s = d->seat;
71fda00f 49 LIST_REMOVE(devices, d->seat->devices, d);
20263082 50 d->seat = NULL;
f1a8e221 51
718d006a
DH
52 if (!seat_has_master_device(s)) {
53 seat_add_to_gc_queue(s);
cc377381 54 seat_send_changed(s, "CanGraphical", NULL);
718d006a 55 }
20263082
LP
56}
57
9588bc32
LP
58void device_free(Device *d) {
59 assert(d);
60
61 device_detach(d);
62
63 hashmap_remove(d->manager->devices, d->sysfs);
64
65 free(d->sysfs);
66 free(d);
67}
68
20263082 69void device_attach(Device *d, Seat *s) {
718d006a
DH
70 Device *i;
71 bool had_master;
72
20263082
LP
73 assert(d);
74 assert(s);
75
f1a8e221
LP
76 if (d->seat == s)
77 return;
78
20263082
LP
79 if (d->seat)
80 device_detach(d);
81
20263082 82 d->seat = s;
718d006a
DH
83 had_master = seat_has_master_device(s);
84
85 /* We keep the device list sorted by the "master" flag. That is, master
86 * devices are at the front, other devices at the tail. As there is no
87 * way to easily add devices at the list-tail, we need to iterate the
88 * list to find the first non-master device when adding non-master
89 * devices. We assume there is only a few (normally 1) master devices
90 * per seat, so we iterate only a few times. */
91
92 if (d->master || !s->devices)
71fda00f 93 LIST_PREPEND(devices, s->devices, d);
4b9e5848 94 else
718d006a
DH
95 LIST_FOREACH(devices, i, s->devices) {
96 if (!i->devices_next || !i->master) {
71fda00f 97 LIST_INSERT_AFTER(devices, s->devices, i, d);
718d006a
DH
98 break;
99 }
100 }
f1a8e221 101
ad1bf59c
RS
102 if (!had_master && d->master && s->started) {
103 seat_save(s);
cc377381 104 seat_send_changed(s, "CanGraphical", NULL);
ad1bf59c 105 }
20263082 106}