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