Skip to content

Commit 07bdff9

Browse files
authored
Merge pull request #263 from rtbo/fix_display_info
Fix DisplayInfo
2 parents 2a91345 + d4b6da4 commit 07bdff9

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

src/base.rs

+44-24
Original file line numberDiff line numberDiff line change
@@ -459,14 +459,14 @@ pub struct AuthInfo<'a> {
459459
}
460460

461461
/// Display info returned by [`parse_display`]
462-
#[derive(Debug)]
462+
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
463463
pub struct DisplayInfo {
464464
/// The hostname
465-
host: String,
465+
pub host: String,
466466
/// The display number
467-
display: i32,
467+
pub display: i32,
468468
/// The screen number
469-
screen: i32,
469+
pub screen: i32,
470470
}
471471

472472
/// Parses a display string in the form documented by [X (7x)](https://linux.die.net/man/7/x).
@@ -477,36 +477,56 @@ pub struct DisplayInfo {
477477
/// If `name` empty, it uses the environment variable `DISPLAY`.
478478
///
479479
/// If `name` does not contain a screen number, `DisplayInfo::screen` is set to `0`.
480+
///
481+
/// # Example
482+
/// ```
483+
/// use xcb::{DisplayInfo, parse_display};
484+
///
485+
/// assert_eq!(parse_display(":0"), Some(DisplayInfo {
486+
/// host: "".to_string(),
487+
/// display: 0,
488+
/// screen: 0,
489+
/// }));
490+
/// assert_eq!(parse_display("localhost:0.1"), Some(DisplayInfo {
491+
/// host: "localhost".to_string(),
492+
/// display: 0,
493+
/// screen: 1,
494+
/// }));
495+
///
496+
/// assert!(parse_display("0").is_none());
497+
/// ```
480498
pub fn parse_display(name: &str) -> Option<DisplayInfo> {
481-
unsafe {
482-
let name = CString::new(name).unwrap();
483-
let mut hostp: *mut c_char = ptr::null_mut();
484-
let mut display = 0i32;
485-
let mut screen = 0i32;
499+
let name = CString::new(name).unwrap();
500+
let mut hostp: *mut c_char = ptr::null_mut();
501+
let mut display = 0i32;
502+
let mut screen = 0i32;
486503

487-
let success = xcb_parse_display(
504+
let success = unsafe {
505+
xcb_parse_display(
488506
name.as_ptr(),
489507
&mut hostp as *mut _,
490508
&mut display as *mut _,
491509
&mut screen as *mut _,
492-
);
510+
)
511+
};
493512

494-
if success != 0 {
495-
let host = CStr::from_ptr(hostp as *const _)
496-
.to_str()
497-
.unwrap()
498-
.to_string();
513+
if success != 0 {
514+
let host = unsafe { CStr::from_ptr(hostp as *const _) }
515+
.to_str()
516+
.unwrap()
517+
.to_string();
499518

519+
unsafe {
500520
libc::free(hostp as *mut _);
501-
502-
Some(DisplayInfo {
503-
host,
504-
display,
505-
screen,
506-
})
507-
} else {
508-
None
509521
}
522+
523+
Some(DisplayInfo {
524+
host,
525+
display,
526+
screen,
527+
})
528+
} else {
529+
None
510530
}
511531
}
512532

0 commit comments

Comments
 (0)