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