<commit_before>#![cfg(target_os = "linux")]

pub mod ffi;

use std::ptr;
use std::ffi::CString;

use error::{RuntimeError, ErrorKind};

use super::xcb::Connection;

pub struct Display {
   pub ptr: *mut ffi::Display
}

impl Display {
   pub fn default() -> Result<Self, RuntimeError> {
      let display_ptr = unsafe {
         ffi::XOpenDisplay(ptr::null())
      };

      if display_ptr.is_null() {
         return Err(RuntimeError::new(
            ErrorKind::Xlib,
            "Opening default X display failed".to_string()
         ));
      }

      Ok(
         Display {
            ptr: display_ptr
         }
      )
   }

   pub fn new(name: &str) -> Result<Self, RuntimeError> {
      let c_name = CString::new(name).unwrap();

      let display_ptr = unsafe {
         ffi::XOpenDisplay(c_name.as_ptr())
      };

      if display_ptr.is_null() {
         let description = format!(
            "Opening X display '{}' failed", name
         );
         return Err(RuntimeError::new(
            ErrorKind::Xlib,
            description
         ));
      }

      Ok(
         Display {
            ptr: display_ptr
         }
      )
   }

   pub fn xcb_connection(&self) -> Result<Connection, RuntimeError> {
      let connection = unsafe {
         ffi::XGetXCBConnection(self.ptr)
      };

      if connection.is_null() {
         return Err(RuntimeError::new(
            ErrorKind::Xlib,
            "Getting XCB connection failed".to_string()
         ));
      }

      Ok(
         Connection::new(connection)
      )
   }

   pub fn xcb_own_event_queue(&self) {
      unsafe {
         ffi::XSetEventQueueOwner(
            self.ptr,
            ffi::XCBOwnsEventQueue
         )
      };
   }

   #[inline]
   pub fn default_screen(&self) -> ScreenID {
      let screen = unsafe {
         (*(self.ptr as ffi::_XPrivDisplay)).default_screen
      };

      ScreenID {
         screen: screen
      }
   }
}

impl Drop for Display {
   fn drop (&mut self) {
      unsafe {
         ffi::XCloseDisplay(self.ptr);
      }
   }
}

pub struct ScreenID {
   pub screen: ffi::c_int
}

#[cfg(test)]
mod test {
   use std::env;
   use super::Display;

   #[test]
   fn test_create_display() {
      assert!(Display::default().is_ok());
   }

   #[test]
   fn test_create_display_from_env() {
      let name = env::var("DISPLAY").unwrap();
      assert!(Display::new(name).is_ok());
   }

   #[test]
   fn test_create_display_error_str() {
      assert!(Display::new("ErrorStringHere").is_err());
   }

   #[test]
   fn test_create_display_nul_str() {
      assert!(Display::new(&b"one\0two"[..]).is_err());
   }

   #[test]
   fn test_create_get_xcb_connection() {
      assert!(Display::default().unwrap().xcb_connection().is_ok());
   }
}
<commit_msg>Fixing X11 tests<commit_after>#![cfg(target_os = "linux")]

pub mod ffi;

use std::ptr;
use std::ffi::CString;

use error::{RuntimeError, ErrorKind};

use super::xcb::Connection;

pub struct Display {
   pub ptr: *mut ffi::Display
}

impl Display {
   pub fn default() -> Result<Self, RuntimeError> {
      let display_ptr = unsafe {
         ffi::XOpenDisplay(ptr::null())
      };

      if display_ptr.is_null() {
         return Err(RuntimeError::new(
            ErrorKind::Xlib,
            "Opening default X display failed".to_string()
         ));
      }

      Ok(
         Display {
            ptr: display_ptr
         }
      )
   }

   pub fn new(name: &str) -> Result<Self, RuntimeError> {
      let c_name = CString::new(name).unwrap();

      let display_ptr = unsafe {
         ffi::XOpenDisplay(c_name.as_ptr())
      };

      if display_ptr.is_null() {
         let description = format!(
            "Opening X display '{}' failed", name
         );
         return Err(RuntimeError::new(
            ErrorKind::Xlib,
            description
         ));
      }

      Ok(
         Display {
            ptr: display_ptr
         }
      )
   }

   pub fn xcb_connection(&self) -> Result<Connection, RuntimeError> {
      let connection = unsafe {
         ffi::XGetXCBConnection(self.ptr)
      };

      if connection.is_null() {
         return Err(RuntimeError::new(
            ErrorKind::Xlib,
            "Getting XCB connection failed".to_string()
         ));
      }

      Ok(
         Connection::new(connection)
      )
   }

   pub fn xcb_own_event_queue(&self) {
      unsafe {
         ffi::XSetEventQueueOwner(
            self.ptr,
            ffi::XCBOwnsEventQueue
         )
      };
   }

   #[inline]
   pub fn default_screen(&self) -> ScreenID {
      let screen = unsafe {
         (*(self.ptr as ffi::_XPrivDisplay)).default_screen
      };

      ScreenID {
         screen: screen
      }
   }
}

impl Drop for Display {
   fn drop (&mut self) {
      unsafe {
         ffi::XCloseDisplay(self.ptr);
      }
   }
}

pub struct ScreenID {
   pub screen: ffi::c_int
}

#[cfg(test)]
mod test {
   use std::env;
   use super::Display;

   #[test]
   fn test_create_display() {
      assert!(Display::default().is_ok());
   }

   #[test]
   fn test_create_display_from_env() {
      let name = env::var("DISPLAY").unwrap();
      assert!(Display::new(name.as_ref()).is_ok());
   }

   #[test]
   fn test_create_display_error_str() {
      assert!(Display::new("ErrorStringHere").is_err());
   }

   #[test]
   fn test_create_display_nul_str() {
      assert!(Display::new("one\0two").is_err());
   }

   #[test]
   fn test_create_get_xcb_connection() {
      assert!(Display::default().unwrap().xcb_connection().is_ok());
   }
}

{'fraction_non_alphanumeric': 0.138788752703677, 'fraction_numerical': 0.0007209805335255948, 'mean_word_length': 2.0489010989010987, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4695664', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1786, 'n_words': 330}