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