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