Skip to content

Commit 4df09e1

Browse files
committed
Removed crash-prone unwraps. Fixes #202
1 parent 90f349b commit 4df09e1

File tree

5 files changed

+68
-65
lines changed

5 files changed

+68
-65
lines changed

src/brightness_backend/brightnessctl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl VirtualDevice {
5858
Ok(s)
5959
} else {
6060
bail!(DeviceDoesntExistError {
61-
device_name: device_name.unwrap()
61+
device_name: device_name.unwrap_or("Device name unknown".to_string())
6262
})
6363
}
6464
}

src/input-backend/main.rs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ fn main() -> Result<(), zbus::Error> {
6060
.expect("Could not assign seat0");
6161
let fd = input.as_raw_fd();
6262
assert!(fd != -1);
63-
let borrowed_fd = unsafe { BorrowedFd::borrow_raw(input.as_raw_fd()) };
63+
let borrowed_fd = unsafe { BorrowedFd::borrow_raw(fd) };
6464
let pollfd = PollFd::new(borrowed_fd, PollFlags::POLLIN);
6565
while poll(&mut [pollfd.clone()], None::<u8>).is_ok() {
66-
event(&input_config, &mut input, &iface_ref);
66+
if let Err(error) = event(&input_config, &mut input, &iface_ref) {
67+
eprintln!("Event error: {:?}", error);
68+
}
6769
}
6870

6971
Ok(())
@@ -73,19 +75,22 @@ fn event(
7375
input_config: &config::backend::InputBackendConfig,
7476
input: &mut Libinput,
7577
iface_ref: &InterfaceRef<DbusServer>,
76-
) {
77-
input.dispatch().unwrap();
78-
for event in input.into_iter() {
79-
if let Event::Keyboard(KeyboardEvent::Key(event)) = event {
80-
if event.key_state() == KeyState::Pressed {
81-
continue;
82-
}
83-
let device = match unsafe { event.device().udev_device() } {
84-
Some(device) => device,
85-
None => continue,
86-
};
78+
) -> Result<(), Box<dyn std::error::Error>> {
79+
input.dispatch()?;
80+
for event in input {
81+
let event = match event {
82+
Event::Keyboard(KeyboardEvent::Key(event)) => event,
83+
_ => continue,
84+
};
85+
if event.key_state() == KeyState::Pressed {
86+
continue;
87+
}
88+
let device = match unsafe { event.device().udev_device() } {
89+
Some(device) => device,
90+
None => continue,
91+
};
8792

88-
let ev_key = match int_to_ev_key(event.key()) {
93+
let ev_key = match int_to_ev_key(event.key()) {
8994
// Basic Lock keys
9095
Some(key @ EV_KEY::KEY_CAPSLOCK) |
9196
Some(key @ EV_KEY::KEY_NUMLOCK) |
@@ -122,24 +127,23 @@ fn event(
122127
_ => continue,
123128
};
124129

125-
// Special case because several people have the caps lock key
126-
// bound to escape, so it doesn't affect the caps lock status
127-
if ev_key == EV_KEY::KEY_CAPSLOCK && input_config.ignore_caps_lock_key.unwrap_or(false)
128-
{
129-
continue;
130-
}
131-
132-
if let Some(path) = device.devnode()
133-
&& let Some(path) = path.to_str()
134-
{
135-
let event_info = EventInfo {
136-
device_path: path.to_owned(),
137-
ev_key,
138-
};
139-
task::spawn(call(event_info, iface_ref.clone()));
140-
}
130+
// Special case because several people have the caps lock key
131+
// bound to escape, so it doesn't affect the caps lock status
132+
if ev_key == EV_KEY::KEY_CAPSLOCK && input_config.ignore_caps_lock_key.unwrap_or(false) {
133+
continue;
134+
}
135+
136+
if let Some(path) = device.devnode()
137+
&& let Some(path) = path.to_str()
138+
{
139+
let event_info = EventInfo {
140+
device_path: path.to_owned(),
141+
ev_key,
142+
};
143+
task::spawn(call(event_info, iface_ref.clone()));
141144
}
142145
}
146+
Ok(())
143147
}
144148

145149
async fn call(event_info: EventInfo, iface_ref: InterfaceRef<DbusServer>) {

src/server/application.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,13 @@ impl SwayOSDApplication {
9090
server_config,
9191
async move {
9292
while let Ok((arg_type, data)) = action_receiver.recv().await {
93-
osd_app.action_activated(
93+
if let Err(error) = osd_app.action_activated(
9494
server_config.clone(),
9595
arg_type,
9696
(!data.is_empty()).then_some(data),
97-
);
97+
) {
98+
eprintln!("Could not activate action: {:?}", error)
99+
}
98100
}
99101
Break
100102
}
@@ -133,7 +135,11 @@ impl SwayOSDApplication {
133135
}
134136
_ => continue,
135137
};
136-
osd_app.action_activated(server_config.clone(), arg_type, data);
138+
if let Err(error) =
139+
osd_app.action_activated(server_config.clone(), arg_type, data)
140+
{
141+
eprintln!("Could not activate action: {:?}", error)
142+
}
137143
}
138144
Break
139145
}
@@ -192,11 +198,13 @@ impl SwayOSDApplication {
192198
// (automatically or through a firmware-handled hotkey being pressed)
193199
continue;
194200
}
195-
self.action_activated(
201+
if let Err(error) = self.action_activated(
196202
server_config.clone(),
197203
ArgTypes::KbdBacklight,
198204
Some(format!("{}:{}", args.value, max_brightness)),
199-
);
205+
) {
206+
eprintln!("Could not activate action: {:?}", error)
207+
}
200208
} else {
201209
eprintln!("UPower args aren't valid {:?}", msg.args());
202210
}
@@ -386,10 +394,10 @@ impl SwayOSDApplication {
386394
server_config: Arc<ServerConfig>,
387395
arg_type: ArgTypes,
388396
value: Option<String>,
389-
) {
397+
) -> Result<(), Box<dyn std::error::Error>> {
390398
match (arg_type, value) {
391399
(ArgTypes::SinkVolumeRaise, step) => {
392-
let mut device_type = VolumeDeviceType::Sink(SinkController::create().unwrap());
400+
let mut device_type = VolumeDeviceType::Sink(SinkController::create()?);
393401
if let Some(device) =
394402
change_device_volume(&mut device_type, VolumeChangeType::Raise, step)
395403
{
@@ -402,7 +410,7 @@ impl SwayOSDApplication {
402410
reset_monitor_name();
403411
}
404412
(ArgTypes::SinkVolumeLower, step) => {
405-
let mut device_type = VolumeDeviceType::Sink(SinkController::create().unwrap());
413+
let mut device_type = VolumeDeviceType::Sink(SinkController::create()?);
406414
if let Some(device) =
407415
change_device_volume(&mut device_type, VolumeChangeType::Lower, step)
408416
{
@@ -415,7 +423,7 @@ impl SwayOSDApplication {
415423
reset_monitor_name();
416424
}
417425
(ArgTypes::SinkVolumeMuteToggle, _) => {
418-
let mut device_type = VolumeDeviceType::Sink(SinkController::create().unwrap());
426+
let mut device_type = VolumeDeviceType::Sink(SinkController::create()?);
419427
if let Some(device) =
420428
change_device_volume(&mut device_type, VolumeChangeType::MuteToggle, None)
421429
{
@@ -428,7 +436,7 @@ impl SwayOSDApplication {
428436
reset_monitor_name();
429437
}
430438
(ArgTypes::SourceVolumeRaise, step) => {
431-
let mut device_type = VolumeDeviceType::Source(SourceController::create().unwrap());
439+
let mut device_type = VolumeDeviceType::Source(SourceController::create()?);
432440
if let Some(device) =
433441
change_device_volume(&mut device_type, VolumeChangeType::Raise, step)
434442
{
@@ -441,7 +449,7 @@ impl SwayOSDApplication {
441449
reset_monitor_name();
442450
}
443451
(ArgTypes::SourceVolumeLower, step) => {
444-
let mut device_type = VolumeDeviceType::Source(SourceController::create().unwrap());
452+
let mut device_type = VolumeDeviceType::Source(SourceController::create()?);
445453
if let Some(device) =
446454
change_device_volume(&mut device_type, VolumeChangeType::Lower, step)
447455
{
@@ -454,7 +462,7 @@ impl SwayOSDApplication {
454462
reset_monitor_name();
455463
}
456464
(ArgTypes::SourceVolumeMuteToggle, _) => {
457-
let mut device_type = VolumeDeviceType::Source(SourceController::create().unwrap());
465+
let mut device_type = VolumeDeviceType::Source(SourceController::create()?);
458466
if let Some(device) =
459467
change_device_volume(&mut device_type, VolumeChangeType::MuteToggle, None)
460468
{
@@ -468,12 +476,9 @@ impl SwayOSDApplication {
468476
}
469477
// TODO: Brightness
470478
(ArgTypes::BrightnessRaise, step) => {
471-
if let Ok(mut brightness_backend) =
472-
change_brightness(BrightnessChangeType::Raise, step)
473-
{
474-
for window in self.choose_windows() {
475-
window.changed_brightness(brightness_backend.as_mut());
476-
}
479+
let mut brightness_backend = change_brightness(BrightnessChangeType::Raise, step)?;
480+
for window in self.choose_windows() {
481+
window.changed_brightness(brightness_backend.as_mut());
477482
}
478483
reset_min_brightness();
479484
reset_monitor_name();
@@ -490,12 +495,9 @@ impl SwayOSDApplication {
490495
reset_monitor_name();
491496
}
492497
(ArgTypes::BrightnessSet, value) => {
493-
if let Ok(mut brightness_backend) =
494-
change_brightness(BrightnessChangeType::Set, value)
495-
{
496-
for window in self.choose_windows() {
497-
window.changed_brightness(brightness_backend.as_mut());
498-
}
498+
let mut brightness_backend = change_brightness(BrightnessChangeType::Set, value)?;
499+
for window in self.choose_windows() {
500+
window.changed_brightness(brightness_backend.as_mut());
499501
}
500502
reset_min_brightness();
501503
reset_monitor_name();
@@ -556,8 +558,7 @@ impl SwayOSDApplication {
556558
(ArgTypes::Player, name) => set_player(name.unwrap_or("".to_string())),
557559
(ArgTypes::Playerctl, value) => {
558560
let value = &value.unwrap_or("".to_string());
559-
560-
let action = PlayerctlAction::from(value).unwrap();
561+
let action = PlayerctlAction::from(value)?;
561562
if let Ok(mut player) = Playerctl::new(action, server_config) {
562563
match player.run() {
563564
Ok(_) => {
@@ -647,5 +648,6 @@ impl SwayOSDApplication {
647648
)
648649
}
649650
};
651+
Ok(())
650652
}
651653
}

src/server/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ fn main() {
103103
});
104104
match get_system_css_path() {
105105
Some(path) => {
106-
provider.load_from_path(path.to_str().unwrap());
106+
provider.load_from_path(path);
107107
gtk::style_context_add_provider_for_display(
108108
&display,
109109
&provider,

src/server/utils.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ pub fn change_brightness(
333333
value.unwrap_or(BRIGHTNESS_CHANGE_DELTA) as u32,
334334
min_brightness,
335335
)?,
336-
BrightnessChangeType::Set => backend.set(value.unwrap() as u32, min_brightness)?,
336+
BrightnessChangeType::Set => backend.set(value? as u32, min_brightness)?,
337337
};
338338

339339
Ok(backend)
@@ -344,18 +344,15 @@ pub fn get_system_css_path() -> Option<PathBuf> {
344344
for path in system_config_dirs() {
345345
paths.push(path.join("swayosd").join("style.css"));
346346
}
347-
347+
// Fallback for Debian/Ubuntu-based distros
348348
paths.push(Path::new("/usr/local/etc/xdg/swaync/style.css").to_path_buf());
349349

350-
let mut path: Option<PathBuf> = None;
351350
for try_path in paths {
352351
if try_path.exists() {
353-
path = Some(try_path);
354-
break;
352+
return Some(try_path.clone());
355353
}
356354
}
357-
358-
path
355+
None
359356
}
360357

361358
pub fn user_style_path(custom_path: Option<PathBuf>) -> Option<String> {

0 commit comments

Comments
 (0)