]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-device.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / login / logind-device.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2011 Lennart Poettering
4 ***/
5
6 #include <string.h>
7
8 #include "alloc-util.h"
9 #include "logind-device.h"
10 #include "util.h"
11
12 Device* device_new(Manager *m, const char *sysfs, bool master) {
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);
23 if (!d->sysfs)
24 return mfree(d);
25
26 if (hashmap_put(m->devices, d->sysfs, d) < 0) {
27 free(d->sysfs);
28 return mfree(d);
29 }
30
31 d->manager = m;
32 d->master = master;
33 dual_timestamp_get(&d->timestamp);
34
35 return d;
36 }
37
38 static void device_detach(Device *d) {
39 Seat *s;
40 SessionDevice *sd;
41
42 assert(d);
43
44 if (!d->seat)
45 return;
46
47 while ((sd = d->session_devices))
48 session_device_free(sd);
49
50 s = d->seat;
51 LIST_REMOVE(devices, d->seat->devices, d);
52 d->seat = NULL;
53
54 if (!seat_has_master_device(s)) {
55 seat_add_to_gc_queue(s);
56 seat_send_changed(s, "CanGraphical", NULL);
57 }
58 }
59
60 void 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
71 void device_attach(Device *d, Seat *s) {
72 Device *i;
73 bool had_master;
74
75 assert(d);
76 assert(s);
77
78 if (d->seat == s)
79 return;
80
81 if (d->seat)
82 device_detach(d);
83
84 d->seat = s;
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)
95 LIST_PREPEND(devices, s->devices, d);
96 else {
97 LIST_FOREACH(devices, i, s->devices) {
98 if (!i->devices_next || !i->master) {
99 LIST_INSERT_AFTER(devices, s->devices, i, d);
100 break;
101 }
102 }
103 }
104
105 if (!had_master && d->master)
106 seat_send_changed(s, "CanGraphical", NULL);
107 }