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