Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::ffi::CString;
- use std::mem;
- use std::ptr;
- use std::slice;
- use x11_dl::glx;
- use x11_dl::glx::Glx;
- use x11_dl::xlib;
- use x11_dl::xlib::Xlib;
- fn main() {
- unsafe {
- // Load the xlib function pointers
- let xlib = Xlib::open().unwrap();
- // Load the glx function pointers
- let glx = Glx::open().unwrap();
- // Open a display connection
- let display = (xlib.XOpenDisplay)(ptr::null());
- // The default screen of the display
- let default_screen = (xlib.XDefaultScreen)(display);
- // The root window of the default screen
- let root_window = (xlib.XRootWindow)(display, default_screen);
- // Choose a framebuffer configuration with the given attributes
- let configs = {
- let mut len = 0;
- let ptr = (glx.glXChooseFBConfig)(
- display,
- default_screen,
- [
- glx::GLX_DOUBLEBUFFER,
- 1,
- glx::GLX_RENDER_TYPE,
- glx::GLX_RGBA_BIT,
- glx::GLX_RED_SIZE,
- 8,
- glx::GLX_GREEN_SIZE,
- 8,
- glx::GLX_BLUE_SIZE,
- 8,
- glx::GLX_ALPHA_SIZE,
- 8,
- 0,
- ]
- .as_mut_ptr(),
- &mut len,
- );
- slice::from_raw_parts(ptr, len as usize)
- };
- // Get a visual that is compatible with the given framebuffer configuration
- let visual_info = (glx.glXGetVisualFromFBConfig)(display, configs[0]);
- let mut attributes = mem::zeroed::<xlib::XSetWindowAttributes>();
- attributes.border_pixel = 0;
- attributes.colormap =
- (xlib.XCreateColormap)(display, root_window, (*visual_info).visual, xlib::AllocNone);
- attributes.event_mask = xlib::ExposureMask | xlib::StructureNotifyMask;
- // Create a window
- let window = (xlib.XCreateWindow)(
- display,
- root_window,
- 464,
- 194,
- 512,
- 512,
- 0,
- (*visual_info).depth,
- xlib::InputOutput as u32,
- (*visual_info).visual,
- xlib::CWBorderPixel | xlib::CWColormap | xlib::CWEventMask,
- &mut attributes,
- );
- // Tell the window manager that we want to be notified when the window is closed
- let mut wm_delete_message = (xlib.XInternAtom)(
- display,
- CString::new("WM_DELETE_WINDOW").unwrap().as_ptr(),
- xlib::False,
- );
- (xlib.XSetWMProtocols)(display, window, &mut wm_delete_message, 1);
- // Map the window to the screen
- (xlib.XMapWindow)(display, window);
- // Create a gl context
- let context = (glx.glXCreateContext)(display, visual_info, ptr::null_mut(), 1);
- // Load the gl function pointers
- gl::load_with(|symbol| {
- (glx.glXGetProcAddress)(symbol.as_ptr()).map_or(ptr::null(), |ptr| ptr as *const _)
- });
- // Main event loop
- let mut event = mem::uninitialized();
- loop {
- (xlib.XNextEvent)(display, &mut event);
- match event.type_ {
- xlib::ClientMessage => {
- if event.client_message.data.get_long(0) as u64 == wm_delete_message {
- break;
- }
- }
- xlib::Expose => {
- (glx.glXMakeCurrent)(display, window, context);
- gl::ClearColor(0.0, 0.0, 0.0, 1.0);
- gl::Clear(gl::COLOR_BUFFER_BIT);
- (glx.glXSwapBuffers)(display, window);
- }
- _ => {}
- }
- }
- (xlib.XCloseDisplay)(display);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement