Skip to content

Commit 448689b

Browse files
kekrbyWhatAmISupposedToPutHere
authored andcommitted
Don't hardcode the touchbar width and height
1 parent 08a556e commit 448689b

2 files changed

Lines changed: 38 additions & 31 deletions

File tree

src/display.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use drm::{
77
ClientCapability, Device as DrmDevice, buffer::DrmFourcc,
88
control::{
99
connector, Device as ControlDevice, property, ResourceHandle, atomic, AtomicCommitFlags,
10-
dumbbuffer::{DumbBuffer, DumbMapping}, framebuffer, ClipRect
10+
dumbbuffer::{DumbBuffer, DumbMapping}, framebuffer, ClipRect, Mode
1111
}
1212
};
1313
use anyhow::{Result, anyhow};
@@ -34,6 +34,7 @@ impl Card {
3434

3535
pub struct DrmBackend {
3636
card: Card,
37+
mode: Mode,
3738
db: DumbBuffer,
3839
fb: framebuffer::Handle
3940
}
@@ -169,7 +170,7 @@ fn try_open_card(path: &Path) -> Result<DrmBackend> {
169170
card.atomic_commit(AtomicCommitFlags::ALLOW_MODESET, atomic_req)?;
170171

171172

172-
Ok(DrmBackend { card, db, fb })
173+
Ok(DrmBackend { card, mode, db, fb })
173174
}
174175

175176
impl DrmBackend {
@@ -189,6 +190,12 @@ impl DrmBackend {
189190
}
190191
Err(anyhow!("No touchbar device found, attempted: [\n {}\n]", errors.join(",\n ")))
191192
}
193+
pub fn mode(&self) -> Mode {
194+
self.mode
195+
}
196+
pub fn fb_info(&self) -> Result<framebuffer::Info> {
197+
Ok(self.card.get_framebuffer(self.fb)?)
198+
}
192199
pub fn dirty(&self, clips: &[ClipRect]) -> Result<()> {
193200
Ok(self.card.dirty_framebuffer(self.fb, clips)?)
194201
}

src/main.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ use pixel_shift::{PixelShiftManager, PIXEL_SHIFT_WIDTH_PX};
4545
use config::{ButtonConfig, Config};
4646
use crate::config::ConfigManager;
4747

48-
const DFR_WIDTH: i32 = 2008;
49-
const DFR_HEIGHT: i32 = 60;
50-
const DFR_STRIDE: i32 = 64;
5148
const BUTTON_SPACING_PX: i32 = 16;
5249
const BUTTON_COLOR_INACTIVE: f64 = 0.200;
5350
const BUTTON_COLOR_ACTIVE: f64 = 0.400;
@@ -117,28 +114,28 @@ impl Button {
117114
changed: false,
118115
}
119116
}
120-
fn render(&self, c: &Context, button_left_edge: f64, button_width: u64, y_shift: f64) {
117+
fn render(&self, c: &Context, height: i32, button_left_edge: f64, button_width: u64, y_shift: f64) {
121118
match &self.image {
122119
ButtonImage::Text(text) => {
123120
let extents = c.text_extents(text).unwrap();
124121
c.move_to(
125122
button_left_edge + (button_width as f64 / 2.0 - extents.width() / 2.0).round(),
126-
y_shift + (DFR_HEIGHT as f64 / 2.0 + extents.height() / 2.0).round()
123+
y_shift + (height as f64 / 2.0 + extents.height() / 2.0).round()
127124
);
128125
c.show_text(text).unwrap();
129126
},
130127
ButtonImage::Svg(svg) => {
131128
let renderer = CairoRenderer::new(&svg);
132129
let x = button_left_edge + (button_width as f64 / 2.0 - (ICON_SIZE / 2) as f64).round();
133-
let y = y_shift + ((DFR_HEIGHT as f64 - ICON_SIZE as f64) / 2.0).round();
130+
let y = y_shift + ((height as f64 - ICON_SIZE as f64) / 2.0).round();
134131

135132
renderer.render_document(c,
136133
&Rectangle::new(x, y, ICON_SIZE as f64, ICON_SIZE as f64)
137134
).unwrap();
138135
}
139136
ButtonImage::Bitmap(surf) => {
140137
let x = button_left_edge + (button_width as f64 / 2.0 - (ICON_SIZE / 2) as f64).round();
141-
let y = y_shift + ((DFR_HEIGHT as f64 - ICON_SIZE as f64) / 2.0).round();
138+
let y = y_shift + ((height as f64 - ICON_SIZE as f64) / 2.0).round();
142139
c.set_source_surface(surf, x, y).unwrap();
143140
c.rectangle(x, y, ICON_SIZE as f64, ICON_SIZE as f64);
144141
c.fill().unwrap();
@@ -169,20 +166,20 @@ impl FunctionLayer {
169166
buttons: cfg.into_iter().map(Button::with_config).collect()
170167
}
171168
}
172-
fn draw(&mut self, config: &Config, surface: &Surface, pixel_shift: (f64, f64), complete_redraw: bool) -> Vec<ClipRect> {
169+
fn draw(&mut self, config: &Config, width: i32, height: i32, surface: &Surface, pixel_shift: (f64, f64), complete_redraw: bool) -> Vec<ClipRect> {
173170
let c = Context::new(&surface).unwrap();
174171
let mut modified_regions = if complete_redraw {
175-
vec![ClipRect::new(0, 0, DFR_HEIGHT as u16, DFR_WIDTH as u16)]
172+
vec![ClipRect::new(0, 0, height as u16, width as u16)]
176173
} else {
177174
Vec::new()
178175
};
179-
c.translate(DFR_HEIGHT as f64, 0.0);
176+
c.translate(height as f64, 0.0);
180177
c.rotate((90.0f64).to_radians());
181178
let pixel_shift_width = if config.enable_pixel_shift { PIXEL_SHIFT_WIDTH_PX } else { 0 };
182-
let button_width = ((DFR_WIDTH - pixel_shift_width as i32) - (BUTTON_SPACING_PX * (self.buttons.len() - 1) as i32)) as f64 / self.buttons.len() as f64;
179+
let button_width = ((width - pixel_shift_width as i32) - (BUTTON_SPACING_PX * (self.buttons.len() - 1) as i32)) as f64 / self.buttons.len() as f64;
183180
let radius = 8.0f64;
184-
let bot = (DFR_HEIGHT as f64) * 0.15;
185-
let top = (DFR_HEIGHT as f64) * 0.85;
181+
let bot = (height as f64) * 0.15;
182+
let top = (height as f64) * 0.85;
186183
let (pixel_shift_x, pixel_shift_y) = pixel_shift;
187184

188185
if complete_redraw {
@@ -246,15 +243,15 @@ impl FunctionLayer {
246243

247244
c.fill().unwrap();
248245
c.set_source_rgb(1.0, 1.0, 1.0);
249-
button.render(&c, left_edge, button_width.ceil() as u64, pixel_shift_y);
246+
button.render(&c, height, left_edge, button_width.ceil() as u64, pixel_shift_y);
250247

251248
button.changed = false;
252249

253250
if !complete_redraw {
254251
modified_regions.push(ClipRect::new(
255-
DFR_HEIGHT as u16 - top as u16 - radius as u16,
252+
height as u16 - top as u16 - radius as u16,
256253
left_edge as u16,
257-
DFR_HEIGHT as u16 - bot as u16 + radius as u16,
254+
height as u16 - bot as u16 + radius as u16,
258255
left_edge as u16 + button_width as u16
259256
));
260257
}
@@ -284,13 +281,13 @@ impl LibinputInterface for Interface {
284281
}
285282

286283

287-
fn button_hit(num: u32, idx: u32, x: f64, y: f64) -> bool {
288-
let button_width = (DFR_WIDTH - (BUTTON_SPACING_PX * (num - 1) as i32)) as f64 / num as f64;
284+
fn button_hit(num: u32, idx: u32, width: u16, height: u16, x: f64, y: f64) -> bool {
285+
let button_width = (width as i32 - (BUTTON_SPACING_PX * (num - 1) as i32)) as f64 / num as f64;
289286
let left_edge = idx as f64 * (button_width + BUTTON_SPACING_PX as f64);
290287
if x < left_edge || x > (left_edge + button_width) {
291288
return false
292289
}
293-
y > 0.1 * DFR_HEIGHT as f64 && y < 0.9 * DFR_HEIGHT as f64
290+
y > 0.1 * height as f64 && y < 0.9 * height as f64
294291
}
295292

296293
fn emit<F>(uinput: &mut UInputHandle<F>, ty: EventKind, code: u16, value: i32) where F: AsRawFd {
@@ -312,6 +309,7 @@ fn toggle_key<F>(uinput: &mut UInputHandle<F>, code: Key, value: i32) where F: A
312309

313310
fn main() {
314311
let mut drm = DrmBackend::open_card().unwrap();
312+
let (height, width) = drm.mode().size();
315313
let _ = panic::catch_unwind(AssertUnwindSafe(|| {
316314
real_main(&mut drm)
317315
}));
@@ -331,13 +329,15 @@ fn main() {
331329
}
332330
}
333331
drop(map);
334-
drm.dirty(&[ClipRect::new(0, 0, DFR_HEIGHT as u16, DFR_WIDTH as u16)]).unwrap();
332+
drm.dirty(&[ClipRect::new(0, 0, height as u16, width as u16)]).unwrap();
335333
let mut sigset = SigSet::empty();
336334
sigset.add(Signal::SIGTERM);
337335
sigset.wait().unwrap();
338336
}
339337

340338
fn real_main(drm: &mut DrmBackend) {
339+
let (height, width) = drm.mode().size();
340+
let (db_width, db_height) = drm.fb_info().unwrap().size();
341341
let mut uinput = UInputHandle::new(OpenOptions::new().write(true).open("/dev/uinput").unwrap());
342342
let mut backlight = BacklightManager::new();
343343
let mut cfg_mgr = ConfigManager::new();
@@ -353,7 +353,7 @@ fn real_main(drm: &mut DrmBackend) {
353353
.apply()
354354
.unwrap_or_else(|e| { panic!("Failed to drop privileges: {}", e) });
355355

356-
let mut surface = ImageSurface::create(Format::ARgb32, DFR_STRIDE, DFR_WIDTH).unwrap();
356+
let mut surface = ImageSurface::create(Format::ARgb32, db_width as i32, db_height as i32).unwrap();
357357
let mut active_layer = 0;
358358
let mut needs_complete_redraw = true;
359359

@@ -411,7 +411,7 @@ fn real_main(drm: &mut DrmBackend) {
411411
} else {
412412
(0.0, 0.0)
413413
};
414-
let clips = layers[active_layer].draw(&cfg, &surface, shift, needs_complete_redraw);
414+
let clips = layers[active_layer].draw(&cfg, width as i32, height as i32, &surface, shift, needs_complete_redraw);
415415
let data = surface.data().unwrap();
416416
drm.map().unwrap().as_mut()[..data.len()].copy_from_slice(&data);
417417
drm.dirty(&clips).unwrap();
@@ -451,10 +451,10 @@ fn real_main(drm: &mut DrmBackend) {
451451
}
452452
match te {
453453
TouchEvent::Down(dn) => {
454-
let x = dn.x_transformed(DFR_WIDTH as u32);
455-
let y = dn.y_transformed(DFR_HEIGHT as u32);
456-
let btn = (x / (DFR_WIDTH as f64 / layers[active_layer].buttons.len() as f64)) as u32;
457-
if button_hit(layers[active_layer].buttons.len() as u32, btn, x, y) {
454+
let x = dn.x_transformed(width as u32);
455+
let y = dn.y_transformed(height as u32);
456+
let btn = (x / (width as f64 / layers[active_layer].buttons.len() as f64)) as u32;
457+
if button_hit(layers[active_layer].buttons.len() as u32, btn, width, height, x, y) {
458458
touches.insert(dn.seat_slot(), (active_layer, btn));
459459
layers[active_layer].buttons[btn as usize].set_active(&mut uinput, true);
460460
}
@@ -464,10 +464,10 @@ fn real_main(drm: &mut DrmBackend) {
464464
continue;
465465
}
466466

467-
let x = mtn.x_transformed(DFR_WIDTH as u32);
468-
let y = mtn.y_transformed(DFR_HEIGHT as u32);
467+
let x = mtn.x_transformed(width as u32);
468+
let y = mtn.y_transformed(height as u32);
469469
let (layer, btn) = *touches.get(&mtn.seat_slot()).unwrap();
470-
let hit = button_hit(layers[layer].buttons.len() as u32, btn, x, y);
470+
let hit = button_hit(layers[layer].buttons.len() as u32, btn, width, height, x, y);
471471
layers[layer].buttons[btn as usize].set_active(&mut uinput, hit);
472472
},
473473
TouchEvent::Up(up) => {

0 commit comments

Comments
 (0)