Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.91 KB | None | 0 0
  1. use std::ffi::CString;
  2. use std::mem;
  3. use std::ptr;
  4. use std::slice;
  5. use x11_dl::glx;
  6. use x11_dl::glx::Glx;
  7. use x11_dl::xlib;
  8. use x11_dl::xlib::Xlib;
  9.  
  10. fn main() {
  11.     unsafe {
  12.         // Load the xlib function pointers
  13.         let xlib = Xlib::open().unwrap();
  14.  
  15.         // Load the glx function pointers
  16.         let glx = Glx::open().unwrap();
  17.  
  18.         // Open a display connection
  19.         let display = (xlib.XOpenDisplay)(ptr::null());
  20.  
  21.         // The default screen of the display
  22.         let default_screen = (xlib.XDefaultScreen)(display);
  23.  
  24.         // The root window of the default screen
  25.         let root_window = (xlib.XRootWindow)(display, default_screen);
  26.  
  27.         // Choose a framebuffer configuration with the given attributes
  28.         let configs = {
  29.             let mut len = 0;
  30.             let ptr = (glx.glXChooseFBConfig)(
  31.                 display,
  32.                 default_screen,
  33.                 [
  34.                     glx::GLX_DOUBLEBUFFER,
  35.                     1,
  36.                     glx::GLX_RENDER_TYPE,
  37.                     glx::GLX_RGBA_BIT,
  38.                     glx::GLX_RED_SIZE,
  39.                     8,
  40.                     glx::GLX_GREEN_SIZE,
  41.                     8,
  42.                     glx::GLX_BLUE_SIZE,
  43.                     8,
  44.                     glx::GLX_ALPHA_SIZE,
  45.                     8,
  46.                     0,
  47.                 ]
  48.                 .as_mut_ptr(),
  49.                 &mut len,
  50.             );
  51.             slice::from_raw_parts(ptr, len as usize)
  52.         };
  53.  
  54.         // Get a visual that is compatible with the given framebuffer configuration
  55.         let visual_info = (glx.glXGetVisualFromFBConfig)(display, configs[0]);
  56.  
  57.         let mut attributes = mem::zeroed::<xlib::XSetWindowAttributes>();
  58.         attributes.border_pixel = 0;
  59.         attributes.colormap =
  60.             (xlib.XCreateColormap)(display, root_window, (*visual_info).visual, xlib::AllocNone);
  61.         attributes.event_mask = xlib::ExposureMask | xlib::StructureNotifyMask;
  62.  
  63.         // Create a window
  64.         let window = (xlib.XCreateWindow)(
  65.             display,
  66.             root_window,
  67.             464,
  68.             194,
  69.             512,
  70.             512,
  71.             0,
  72.             (*visual_info).depth,
  73.             xlib::InputOutput as u32,
  74.             (*visual_info).visual,
  75.             xlib::CWBorderPixel | xlib::CWColormap | xlib::CWEventMask,
  76.             &mut attributes,
  77.         );
  78.  
  79.         // Tell the window manager that we want to be notified when the window is closed
  80.         let mut wm_delete_message = (xlib.XInternAtom)(
  81.             display,
  82.             CString::new("WM_DELETE_WINDOW").unwrap().as_ptr(),
  83.             xlib::False,
  84.         );
  85.         (xlib.XSetWMProtocols)(display, window, &mut wm_delete_message, 1);
  86.  
  87.         // Map the window to the screen
  88.         (xlib.XMapWindow)(display, window);
  89.  
  90.         // Create a gl context
  91.         let context = (glx.glXCreateContext)(display, visual_info, ptr::null_mut(), 1);
  92.  
  93.         // Load the gl function pointers
  94.         gl::load_with(|symbol| {
  95.             (glx.glXGetProcAddress)(symbol.as_ptr()).map_or(ptr::null(), |ptr| ptr as *const _)
  96.         });
  97.  
  98.         // Main event loop
  99.         let mut event = mem::uninitialized();
  100.         loop {
  101.             (xlib.XNextEvent)(display, &mut event);
  102.             match event.type_ {
  103.                 xlib::ClientMessage => {
  104.                     if event.client_message.data.get_long(0) as u64 == wm_delete_message {
  105.                         break;
  106.                     }
  107.                 }
  108.                 xlib::Expose => {
  109.                     (glx.glXMakeCurrent)(display, window, context);
  110.                     gl::ClearColor(0.0, 0.0, 0.0, 1.0);
  111.                     gl::Clear(gl::COLOR_BUFFER_BIT);
  112.                     (glx.glXSwapBuffers)(display, window);
  113.                 }
  114.                 _ => {}
  115.             }
  116.         }
  117.  
  118.         (xlib.XCloseDisplay)(display);
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement