]>
Commit | Line | Data |
---|---|---|
20263082 LP |
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 | |
5430f7f2 LP |
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 | |
20263082 LP |
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 | |
5430f7f2 | 16 | Lesser General Public License for more details. |
20263082 | 17 | |
5430f7f2 | 18 | You should have received a copy of the GNU Lesser General Public License |
20263082 LP |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
20 | ***/ | |
21 | ||
22 | #include <assert.h> | |
23 | #include <string.h> | |
24 | ||
90821c93 | 25 | #include "logind-device.h" |
20263082 LP |
26 | #include "util.h" |
27 | ||
718d006a | 28 | Device* device_new(Manager *m, const char *sysfs, bool master) { |
20263082 LP |
29 | Device *d; |
30 | ||
31 | assert(m); | |
32 | assert(sysfs); | |
33 | ||
34 | d = new0(Device, 1); | |
35 | if (!d) | |
36 | return NULL; | |
37 | ||
38 | d->sysfs = strdup(sysfs); | |
39 | if (!d->sysfs) { | |
40 | free(d); | |
41 | return NULL; | |
42 | } | |
43 | ||
44 | if (hashmap_put(m->devices, d->sysfs, d) < 0) { | |
45 | free(d->sysfs); | |
46 | free(d); | |
47 | return NULL; | |
48 | } | |
49 | ||
50 | d->manager = m; | |
718d006a | 51 | d->master = master; |
20263082 LP |
52 | dual_timestamp_get(&d->timestamp); |
53 | ||
54 | return d; | |
55 | } | |
56 | ||
57 | void device_free(Device *d) { | |
58 | assert(d); | |
59 | ||
60 | device_detach(d); | |
61 | ||
62 | hashmap_remove(d->manager->devices, d->sysfs); | |
63 | ||
64 | free(d->sysfs); | |
65 | free(d); | |
66 | } | |
67 | ||
68 | void device_detach(Device *d) { | |
f1a8e221 | 69 | Seat *s; |
118ecf32 | 70 | SessionDevice *sd; |
20263082 LP |
71 | assert(d); |
72 | ||
f1a8e221 LP |
73 | if (!d->seat) |
74 | return; | |
20263082 | 75 | |
118ecf32 DH |
76 | while ((sd = d->session_devices)) |
77 | session_device_free(sd); | |
78 | ||
f1a8e221 LP |
79 | s = d->seat; |
80 | LIST_REMOVE(Device, devices, d->seat->devices, d); | |
20263082 | 81 | d->seat = NULL; |
f1a8e221 | 82 | |
718d006a DH |
83 | if (!seat_has_master_device(s)) { |
84 | seat_add_to_gc_queue(s); | |
85 | seat_send_changed(s, "CanGraphical\0"); | |
86 | } | |
20263082 LP |
87 | } |
88 | ||
89 | void device_attach(Device *d, Seat *s) { | |
718d006a DH |
90 | Device *i; |
91 | bool had_master; | |
92 | ||
20263082 LP |
93 | assert(d); |
94 | assert(s); | |
95 | ||
f1a8e221 LP |
96 | if (d->seat == s) |
97 | return; | |
98 | ||
20263082 LP |
99 | if (d->seat) |
100 | device_detach(d); | |
101 | ||
20263082 | 102 | d->seat = s; |
718d006a DH |
103 | 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 | if (d->master || !s->devices) | |
113 | LIST_PREPEND(Device, devices, s->devices, d); | |
114 | else { | |
115 | LIST_FOREACH(devices, i, s->devices) { | |
116 | if (!i->devices_next || !i->master) { | |
117 | LIST_INSERT_AFTER(Device, devices, s->devices, i, d); | |
118 | break; | |
119 | } | |
120 | } | |
121 | } | |
f1a8e221 | 122 | |
718d006a DH |
123 | if (!had_master && d->master) |
124 | seat_send_changed(s, "CanGraphical\0"); | |
20263082 | 125 | } |