chiark / gitweb /
Prep v231.2: login/elogind.c: Remove bus_forward_agent_released()
[elogind.git] / src / login / logind-device.c
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
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
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
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <string.h>
21
22 #include "alloc-util.h"
23 #include "logind-device.h"
24 #include "util.h"
25
26 Device* device_new(Manager *m, const char *sysfs, bool master) {
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);
37         if (!d->sysfs) {
38                 free(d);
39                 return NULL;
40         }
41
42         if (hashmap_put(m->devices, d->sysfs, d) < 0) {
43                 free(d->sysfs);
44                 free(d);
45                 return NULL;
46         }
47
48         d->manager = m;
49         d->master = master;
50         dual_timestamp_get(&d->timestamp);
51
52         return d;
53 }
54
55 static void device_detach(Device *d) {
56         Seat *s;
57         SessionDevice *sd;
58
59         assert(d);
60
61         if (!d->seat)
62                 return;
63
64         while ((sd = d->session_devices))
65                 session_device_free(sd);
66
67         s = d->seat;
68         LIST_REMOVE(devices, d->seat->devices, d);
69         d->seat = NULL;
70
71         if (!seat_has_master_device(s)) {
72                 seat_add_to_gc_queue(s);
73                 seat_send_changed(s, "CanGraphical", NULL);
74         }
75 }
76
77 void device_free(Device *d) {
78         assert(d);
79
80         device_detach(d);
81
82         hashmap_remove(d->manager->devices, d->sysfs);
83
84         free(d->sysfs);
85         free(d);
86 }
87
88 void device_attach(Device *d, Seat *s) {
89         Device *i;
90         bool had_master;
91
92         assert(d);
93         assert(s);
94
95         if (d->seat == s)
96                 return;
97
98         if (d->seat)
99                 device_detach(d);
100
101         d->seat = s;
102         had_master = seat_has_master_device(s);
103
104         /* We keep the device list sorted by the "master" flag. That is, master
105          * devices are at the front, other devices at the tail. As there is no
106          * way to easily add devices at the list-tail, we need to iterate the
107          * list to find the first non-master device when adding non-master
108          * devices. We assume there is only a few (normally 1) master devices
109          * per seat, so we iterate only a few times. */
110
111         if (d->master || !s->devices)
112                 LIST_PREPEND(devices, s->devices, d);
113         else {
114                 LIST_FOREACH(devices, i, s->devices) {
115                         if (!i->devices_next || !i->master) {
116                                 LIST_INSERT_AFTER(devices, s->devices, i, d);
117                                 break;
118                         }
119                 }
120         }
121
122         if (!had_master && d->master)
123                 seat_send_changed(s, "CanGraphical", NULL);
124 }