]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-device.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / login / logind-device.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2011 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <string.h>
22
23 #include "alloc-util.h"
24 #include "logind-device.h"
25 #include "util.h"
26
27 Device* device_new(Manager *m, const char *sysfs, bool master) {
28 Device *d;
29
30 assert(m);
31 assert(sysfs);
32
33 d = new0(Device, 1);
34 if (!d)
35 return NULL;
36
37 d->sysfs = strdup(sysfs);
38 if (!d->sysfs)
39 return mfree(d);
40
41 if (hashmap_put(m->devices, d->sysfs, d) < 0) {
42 free(d->sysfs);
43 return mfree(d);
44 }
45
46 d->manager = m;
47 d->master = master;
48 dual_timestamp_get(&d->timestamp);
49
50 return d;
51 }
52
53 static void device_detach(Device *d) {
54 Seat *s;
55 SessionDevice *sd;
56
57 assert(d);
58
59 if (!d->seat)
60 return;
61
62 while ((sd = d->session_devices))
63 session_device_free(sd);
64
65 s = d->seat;
66 LIST_REMOVE(devices, d->seat->devices, d);
67 d->seat = NULL;
68
69 if (!seat_has_master_device(s)) {
70 seat_add_to_gc_queue(s);
71 seat_send_changed(s, "CanGraphical", NULL);
72 }
73 }
74
75 void device_free(Device *d) {
76 assert(d);
77
78 device_detach(d);
79
80 hashmap_remove(d->manager->devices, d->sysfs);
81
82 free(d->sysfs);
83 free(d);
84 }
85
86 void device_attach(Device *d, Seat *s) {
87 Device *i;
88 bool had_master;
89
90 assert(d);
91 assert(s);
92
93 if (d->seat == s)
94 return;
95
96 if (d->seat)
97 device_detach(d);
98
99 d->seat = s;
100 had_master = seat_has_master_device(s);
101
102 /* We keep the device list sorted by the "master" flag. That is, master
103 * devices are at the front, other devices at the tail. As there is no
104 * way to easily add devices at the list-tail, we need to iterate the
105 * list to find the first non-master device when adding non-master
106 * devices. We assume there is only a few (normally 1) master devices
107 * per seat, so we iterate only a few times. */
108
109 if (d->master || !s->devices)
110 LIST_PREPEND(devices, s->devices, d);
111 else {
112 LIST_FOREACH(devices, i, s->devices) {
113 if (!i->devices_next || !i->master) {
114 LIST_INSERT_AFTER(devices, s->devices, i, d);
115 break;
116 }
117 }
118 }
119
120 if (!had_master && d->master)
121 seat_send_changed(s, "CanGraphical", NULL);
122 }