]>
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" |
6ad1d1ed | 6 | #include "hashmap.h" |
214c93c8 | 7 | #include "logind.h" |
cc377381 | 8 | #include "logind-device.h" |
214c93c8 | 9 | #include "logind-seat.h" |
6ecda0fb | 10 | #include "logind-seat-dbus.h" |
214c93c8 | 11 | #include "logind-session-device.h" |
20263082 | 12 | |
718d006a | 13 | Device* device_new(Manager *m, const char *sysfs, bool master) { |
20263082 LP |
14 | Device *d; |
15 | ||
16 | assert(m); | |
17 | assert(sysfs); | |
18 | ||
19 | d = new0(Device, 1); | |
20 | if (!d) | |
21 | return NULL; | |
22 | ||
23 | d->sysfs = strdup(sysfs); | |
6b430fdb ZJS |
24 | if (!d->sysfs) |
25 | return mfree(d); | |
20263082 LP |
26 | |
27 | if (hashmap_put(m->devices, d->sysfs, d) < 0) { | |
28 | free(d->sysfs); | |
6b430fdb | 29 | return mfree(d); |
20263082 LP |
30 | } |
31 | ||
32 | d->manager = m; | |
718d006a | 33 | d->master = master; |
fa5a0251 | 34 | dual_timestamp_now(&d->timestamp); |
20263082 LP |
35 | |
36 | return d; | |
37 | } | |
38 | ||
9588bc32 | 39 | static void device_detach(Device *d) { |
f1a8e221 | 40 | Seat *s; |
118ecf32 | 41 | SessionDevice *sd; |
cc377381 | 42 | |
20263082 LP |
43 | assert(d); |
44 | ||
f1a8e221 LP |
45 | if (!d->seat) |
46 | return; | |
20263082 | 47 | |
118ecf32 DH |
48 | while ((sd = d->session_devices)) |
49 | session_device_free(sd); | |
50 | ||
f1a8e221 | 51 | s = d->seat; |
71fda00f | 52 | LIST_REMOVE(devices, d->seat->devices, d); |
20263082 | 53 | d->seat = NULL; |
f1a8e221 | 54 | |
718d006a DH |
55 | if (!seat_has_master_device(s)) { |
56 | seat_add_to_gc_queue(s); | |
38c9ca53 | 57 | seat_send_changed(s, "CanGraphical"); |
718d006a | 58 | } |
20263082 LP |
59 | } |
60 | ||
9588bc32 LP |
61 | void device_free(Device *d) { |
62 | assert(d); | |
63 | ||
64 | device_detach(d); | |
65 | ||
66 | hashmap_remove(d->manager->devices, d->sysfs); | |
67 | ||
68 | free(d->sysfs); | |
69 | free(d); | |
70 | } | |
71 | ||
20263082 | 72 | void device_attach(Device *d, Seat *s) { |
718d006a DH |
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); |
4b9e5848 | 96 | else |
718d006a DH |
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 | } | |
f1a8e221 | 103 | |
ad1bf59c RS |
104 | if (!had_master && d->master && s->started) { |
105 | seat_save(s); | |
38c9ca53 | 106 | seat_send_changed(s, "CanGraphical"); |
ad1bf59c | 107 | } |
20263082 | 108 | } |