Skip to content

Commit 5f301e7

Browse files
Search for touchbar card instead of using a hardcoded path
1 parent fbed272 commit 5f301e7

1 file changed

Lines changed: 45 additions & 31 deletions

File tree

src/main.rs

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::{
2-
fs::{File, OpenOptions},
2+
fs::{File, OpenOptions, self},
33
os::{
44
fd::AsRawFd,
55
unix::{io::{AsFd, BorrowedFd, OwnedFd}, fs::OpenOptionsExt}
66
},
7-
path::Path,
7+
path::{Path, PathBuf},
88
collections::HashMap
99
};
1010
use cairo::{
@@ -46,7 +46,7 @@ impl ControlDevice for Card {}
4646
impl DrmDevice for Card {}
4747

4848
impl Card {
49-
fn open(path: &str) -> Self {
49+
fn open(path: PathBuf) -> Self {
5050
let mut options = OpenOptions::new();
5151
options.read(true);
5252
options.write(true);
@@ -112,22 +112,22 @@ fn find_prop_id<T: ResourceHandle>(
112112
) -> Result<property::Handle> {
113113
let props = card.get_properties(handle)?;
114114
for id in props.as_props_and_values().0 {
115-
let info = card.get_property(*id).unwrap();
115+
let info = card.get_property(*id)?;
116116
if info.name().to_str()? == name {
117117
return Ok(*id);
118118
}
119119
}
120120
return Err(anyhow!("Property not found"));
121121
}
122122

123-
fn try_open_card(path: &str) -> Result<DrmBackend> {
123+
fn try_open_card(path: PathBuf) -> Result<DrmBackend> {
124124
let card = Card::open(path);
125-
card.set_client_capability(ClientCapability::UniversalPlanes, true).unwrap();
126-
card.set_client_capability(ClientCapability::Atomic, true).unwrap();
127-
card.acquire_master_lock().unwrap();
125+
card.set_client_capability(ClientCapability::UniversalPlanes, true)?;
126+
card.set_client_capability(ClientCapability::Atomic, true)?;
127+
card.acquire_master_lock()?;
128128

129129

130-
let res = card.resource_handles().unwrap();
130+
let res = card.resource_handles()?;
131131
let coninfo = res
132132
.connectors()
133133
.iter()
@@ -142,95 +142,109 @@ fn try_open_card(path: &str) -> Result<DrmBackend> {
142142
let con = coninfo
143143
.iter()
144144
.find(|&i| i.state() == connector::State::Connected)
145-
.ok_or(anyhow!("No connected connectors found")).unwrap();
145+
.ok_or(anyhow!("No connected connectors found"))?;
146146

147-
let &mode = con.modes().get(0).ok_or(anyhow!("No modes found")).unwrap();
147+
let &mode = con.modes().get(0).ok_or(anyhow!("No modes found"))?;
148148
let (disp_width, disp_height) = mode.size();
149149
if disp_height / disp_width < 30 {
150150
return Err(anyhow!("This does not look like a touchbar"));
151151
}
152-
let crtc = crtcinfo.get(0).ok_or(anyhow!("No crtcs found")).unwrap();
152+
let crtc = crtcinfo.get(0).ok_or(anyhow!("No crtcs found"))?;
153153
let fmt = DrmFourcc::Xrgb8888;
154-
let db = card.create_dumb_buffer((64, disp_height.into()), fmt, 32).unwrap();
154+
let db = card.create_dumb_buffer((64, disp_height.into()), fmt, 32)?;
155155

156-
let fb = card.add_framebuffer(&db, 24, 32).unwrap();
157-
let plane = *card.plane_handles().unwrap().get(0).ok_or(anyhow!("No planes found")).unwrap();
156+
let fb = card.add_framebuffer(&db, 24, 32)?;
157+
let plane = *card.plane_handles()?.get(0).ok_or(anyhow!("No planes found"))?;
158158

159159
let mut atomic_req = atomic::AtomicModeReq::new();
160160
atomic_req.add_property(
161161
con.handle(),
162-
find_prop_id(&card, con.handle(), "CRTC_ID").unwrap(),
162+
find_prop_id(&card, con.handle(), "CRTC_ID")?,
163163
property::Value::CRTC(Some(crtc.handle())),
164164
);
165-
let blob = card.create_property_blob(&mode).unwrap();
165+
let blob = card.create_property_blob(&mode)?;
166166

167167
atomic_req.add_property(
168168
crtc.handle(),
169-
find_prop_id(&card, crtc.handle(), "MODE_ID").unwrap(),
169+
find_prop_id(&card, crtc.handle(), "MODE_ID")?,
170170
blob,
171171
);
172172
atomic_req.add_property(
173173
crtc.handle(),
174-
find_prop_id(&card, crtc.handle(), "ACTIVE").unwrap(),
174+
find_prop_id(&card, crtc.handle(), "ACTIVE")?,
175175
property::Value::Boolean(true),
176176
);
177177
atomic_req.add_property(
178178
plane,
179-
find_prop_id(&card, plane, "FB_ID").unwrap(),
179+
find_prop_id(&card, plane, "FB_ID")?,
180180
property::Value::Framebuffer(Some(fb)),
181181
);
182182
atomic_req.add_property(
183183
plane,
184-
find_prop_id(&card, plane, "CRTC_ID").unwrap(),
184+
find_prop_id(&card, plane, "CRTC_ID")?,
185185
property::Value::CRTC(Some(crtc.handle())),
186186
);
187187
atomic_req.add_property(
188188
plane,
189-
find_prop_id(&card, plane, "SRC_X").unwrap(),
189+
find_prop_id(&card, plane, "SRC_X")?,
190190
property::Value::UnsignedRange(0),
191191
);
192192
atomic_req.add_property(
193193
plane,
194-
find_prop_id(&card, plane, "SRC_Y").unwrap(),
194+
find_prop_id(&card, plane, "SRC_Y")?,
195195
property::Value::UnsignedRange(0),
196196
);
197197
atomic_req.add_property(
198198
plane,
199-
find_prop_id(&card, plane, "SRC_W").unwrap(),
199+
find_prop_id(&card, plane, "SRC_W")?,
200200
property::Value::UnsignedRange((mode.size().0 as u64) << 16),
201201
);
202202
atomic_req.add_property(
203203
plane,
204-
find_prop_id(&card, plane, "SRC_H").unwrap(),
204+
find_prop_id(&card, plane, "SRC_H")?,
205205
property::Value::UnsignedRange((mode.size().1 as u64) << 16),
206206
);
207207
atomic_req.add_property(
208208
plane,
209-
find_prop_id(&card, plane, "CRTC_X").unwrap(),
209+
find_prop_id(&card, plane, "CRTC_X")?,
210210
property::Value::SignedRange(0),
211211
);
212212
atomic_req.add_property(
213213
plane,
214-
find_prop_id(&card, plane, "CRTC_Y").unwrap(),
214+
find_prop_id(&card, plane, "CRTC_Y")?,
215215
property::Value::SignedRange(0),
216216
);
217217
atomic_req.add_property(
218218
plane,
219-
find_prop_id(&card, plane, "CRTC_W").unwrap(),
219+
find_prop_id(&card, plane, "CRTC_W")?,
220220
property::Value::UnsignedRange(mode.size().0 as u64),
221221
);
222222
atomic_req.add_property(
223223
plane,
224-
find_prop_id(&card, plane, "CRTC_H").unwrap(),
224+
find_prop_id(&card, plane, "CRTC_H")?,
225225
property::Value::UnsignedRange(mode.size().1 as u64),
226226
);
227227

228-
card.atomic_commit(AtomicCommitFlags::ALLOW_MODESET, atomic_req).unwrap();
228+
card.atomic_commit(AtomicCommitFlags::ALLOW_MODESET, atomic_req)?;
229229

230230

231231
Ok(DrmBackend { card, db, fb })
232232
}
233233

234+
fn open_card() -> Result<DrmBackend> {
235+
for entry in fs::read_dir("/dev/dri/").unwrap() {
236+
let entry = entry.unwrap();
237+
if !entry.file_name().to_string_lossy().starts_with("card") {
238+
continue
239+
}
240+
match try_open_card(entry.path()) {
241+
Ok(card) => return Ok(card),
242+
Err(_) => {}
243+
}
244+
}
245+
Err(anyhow!("No touchbar device found"))
246+
}
247+
234248

235249
struct Interface;
236250

@@ -292,7 +306,7 @@ fn main() {
292306
};
293307
let mut button_state = vec![false; 12];
294308
let mut needs_redraw = true;
295-
let mut drm = try_open_card("/dev/dri/card0").unwrap();
309+
let mut drm = open_card().unwrap();
296310
let mut input = Libinput::new_with_udev(Interface);
297311
input.udev_assign_seat("seat0").unwrap();
298312
let mut uinput = UInputHandle::new(OpenOptions::new().write(true).open("/dev/uinput").unwrap());

0 commit comments

Comments
 (0)