]>
Commit | Line | Data |
---|---|---|
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" |
20263082 | 8 | |
718d006a | 9 | Device* device_new(Manager *m, const char *sysfs, bool master) { |
20263082 LP |
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); | |
6b430fdb ZJS |
20 | if (!d->sysfs) |
21 | return mfree(d); | |
20263082 LP |
22 | |
23 | if (hashmap_put(m->devices, d->sysfs, d) < 0) { | |
24 | free(d->sysfs); | |
6b430fdb | 25 | return mfree(d); |
20263082 LP |
26 | } |
27 | ||
28 | d->manager = m; | |
718d006a | 29 | d->master = master; |
fa5a0251 | 30 | dual_timestamp_now(&d->timestamp); |
20263082 LP |
31 | |
32 | return d; | |
33 | } | |
34 | ||
9588bc32 | 35 | static void device_detach(Device *d) { |
f1a8e221 | 36 | Seat *s; |
118ecf32 | 37 | SessionDevice *sd; |
cc377381 | 38 | |
20263082 LP |
39 | assert(d); |
40 | ||
f1a8e221 LP |
41 | if (!d->seat) |
42 | return; | |
20263082 | 43 | |
118ecf32 DH |
44 | while ((sd = d->session_devices)) |
45 | session_device_free(sd); | |
46 | ||
f1a8e221 | 47 | s = d->seat; |
71fda00f | 48 | LIST_REMOVE(devices, d->seat->devices, d); |
20263082 | 49 | d->seat = NULL; |
f1a8e221 | 50 | |
718d006a DH |
51 | if (!seat_has_master_device(s)) { |
52 | seat_add_to_gc_queue(s); | |
cc377381 | 53 | seat_send_changed(s, "CanGraphical", NULL); |
718d006a | 54 | } |
20263082 LP |
55 | } |
56 | ||
9588bc32 LP |
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 | ||
20263082 | 68 | void device_attach(Device *d, Seat *s) { |
718d006a DH |
69 | bool had_master; |
70 | ||
20263082 LP |
71 | assert(d); |
72 | assert(s); | |
73 | ||
f1a8e221 LP |
74 | if (d->seat == s) |
75 | return; | |
76 | ||
20263082 LP |
77 | if (d->seat) |
78 | device_detach(d); | |
79 | ||
20263082 | 80 | d->seat = s; |
718d006a DH |
81 | had_master = seat_has_master_device(s); |
82 | ||
83 | /* We keep the device list sorted by the "master" flag. That is, master | |
84 | * devices are at the front, other devices at the tail. As there is no | |
85 | * way to easily add devices at the list-tail, we need to iterate the | |
86 | * list to find the first non-master device when adding non-master | |
87 | * devices. We assume there is only a few (normally 1) master devices | |
88 | * per seat, so we iterate only a few times. */ | |
89 | ||
90 | if (d->master || !s->devices) | |
71fda00f | 91 | LIST_PREPEND(devices, s->devices, d); |
4b9e5848 | 92 | else |
718d006a DH |
93 | LIST_FOREACH(devices, i, s->devices) { |
94 | if (!i->devices_next || !i->master) { | |
71fda00f | 95 | LIST_INSERT_AFTER(devices, s->devices, i, d); |
718d006a DH |
96 | break; |
97 | } | |
98 | } | |
f1a8e221 | 99 | |
ad1bf59c RS |
100 | if (!had_master && d->master && s->started) { |
101 | seat_save(s); | |
cc377381 | 102 | seat_send_changed(s, "CanGraphical", NULL); |
ad1bf59c | 103 | } |
20263082 | 104 | } |