@@ -459,14 +459,14 @@ pub struct AuthInfo<'a> {
459
459
}
460
460
461
461
/// Display info returned by [`parse_display`]
462
- #[ derive( Debug ) ]
462
+ #[ derive( Debug , Clone , PartialEq , Eq , Hash , PartialOrd , Ord ) ]
463
463
pub struct DisplayInfo {
464
464
/// The hostname
465
- host : String ,
465
+ pub host : String ,
466
466
/// The display number
467
- display : i32 ,
467
+ pub display : i32 ,
468
468
/// The screen number
469
- screen : i32 ,
469
+ pub screen : i32 ,
470
470
}
471
471
472
472
/// 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 {
477
477
/// If `name` empty, it uses the environment variable `DISPLAY`.
478
478
///
479
479
/// 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
+ /// ```
480
498
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 ;
486
503
487
- let success = xcb_parse_display (
504
+ let success = unsafe {
505
+ xcb_parse_display (
488
506
name. as_ptr ( ) ,
489
507
& mut hostp as * mut _ ,
490
508
& mut display as * mut _ ,
491
509
& mut screen as * mut _ ,
492
- ) ;
510
+ )
511
+ } ;
493
512
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 ( ) ;
499
518
519
+ unsafe {
500
520
libc:: free ( hostp as * mut _ ) ;
501
-
502
- Some ( DisplayInfo {
503
- host,
504
- display,
505
- screen,
506
- } )
507
- } else {
508
- None
509
521
}
522
+
523
+ Some ( DisplayInfo {
524
+ host,
525
+ display,
526
+ screen,
527
+ } )
528
+ } else {
529
+ None
510
530
}
511
531
}
512
532
0 commit comments