Search
lxdream.org :: lxdream/src/drivers/video_x11.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/video_x11.c
changeset 405:570d93abb5b7
prev370:3131ba1440fc
next424:421d68e78c46
author nkeynes
date Fri Sep 28 07:24:14 2007 +0000 (15 years ago)
permissions -rw-r--r--
last change Add GLSL loader framework
view annotate diff log raw
     1 /**
     2  * $Id: video_x11.c,v 1.14 2007-09-28 07:24:14 nkeynes Exp $
     3  *
     4  * Shared functions for all X11-based display drivers.
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include <X11/Xlib.h>
    20 #include <GL/glx.h>
    21 #include "dream.h"
    22 #include "drivers/video_x11.h"
    23 #include "drivers/gl_common.h"
    25 extern uint32_t video_width, video_height;
    27 /**
    28  * General X11 parameters. The front-end driver is expected to set this up
    29  * by calling video_glx_init after initializing itself.
    30  */
    31 static Display *video_x11_display = NULL;
    32 static Screen *video_x11_screen = NULL;
    33 static Window video_x11_window = 0;
    34 static gboolean glsl_loaded = FALSE;
    36 /**
    37  * GLX parameters.
    38  */
    39 static GLXContext glx_context;
    40 static Window glx_window;
    41 static XSetWindowAttributes win_attrs;
    43 gboolean video_glx_init( Display *display, Screen *screen, Window window,
    44 			 int width, int height, display_driver_t driver )
    45 {
    46     video_x11_display = display;
    47     video_x11_screen = screen;
    48     video_x11_window = window;
    50     if( !video_glx_create_window(width,height) ) {
    51 	return FALSE;
    52     }
    54     if( gl_fbo_is_supported() ) {
    55 	gl_fbo_init(driver);
    57 #ifdef USE_GLSL
    58 	if( glsl_is_supported() ) {
    59 	    glsl_loaded = glsl_load_shaders( glsl_vertex_shader_src, glsl_fragment_shader_src );
    60 	    if( !glsl_loaded ) {
    61 	        WARN( "Shaders failed to load" );
    62 	    }
    63 	} else {
    64 	    WARN( "Shaders not supported" );
    65 	}
    66 #endif
    67 	return TRUE;
    68     } else {
    69 	/* Pbuffers? */
    70 	ERROR( "Framebuffer objects not supported (required in this version)" );
    71 	video_glx_shutdown();
    72 	return FALSE;
    73     }
    74 }
    76 gboolean video_glx_create_window( int width, int height )
    77 {
    78     int major, minor;
    79     const char *glxExts, *glxServer;
    80     int visual_attrs[] = { GLX_RGBA, GLX_RED_SIZE, 4, 
    81 			   GLX_GREEN_SIZE, 4, 
    82 			   GLX_BLUE_SIZE, 4,
    83 			   GLX_ALPHA_SIZE, 4,
    84 			   GLX_DEPTH_SIZE, 24,
    85 			   GLX_DOUBLEBUFFER, 
    86 			   None };
    87     int screen = XScreenNumberOfScreen(video_x11_screen);
    88     XVisualInfo *visual;
    90     if( glXQueryVersion( video_x11_display, &major, &minor ) == False ) {
    91 	ERROR( "X Display lacks the GLX nature" );
    92 	return FALSE;
    93     }
    94     if( major < 1 || minor < 2 ) {
    95 	ERROR( "X display supports GLX %d.%d, but we need at least 1.2", major, minor );
    96 	return FALSE;
    97     }
    99     /* Find ourselves a nice visual */
   100     visual = glXChooseVisual( video_x11_display, 
   101 			      screen,
   102 			      visual_attrs );
   103     if( visual == NULL ) {
   104 	ERROR( "Unable to obtain a compatible visual" );
   105 	return FALSE;
   106     }
   108     /* And a matching gl context */
   109     glx_context = glXCreateContext( video_x11_display, visual, None, True );
   110     if( glx_context == NULL ) {
   111 	ERROR( "Unable to obtain a GLX Context. Possibly your system is broken in some small, undefineable way" );
   112 	return FALSE;
   113     }
   116     /* Ok, all good so far. Unfortunately the visual we need to use will 
   117      * almost certainly be different from the one our frame is using. Which 
   118      * means we have to jump through the following hoops to create a 
   119      * child window with the appropriate settings.
   120      */
   121     win_attrs.event_mask = 0;
   122     win_attrs.colormap = XCreateColormap( video_x11_display, 
   123 					  RootWindowOfScreen(video_x11_screen),
   124 					  visual->visual, AllocNone );
   125     glx_window = XCreateWindow( video_x11_display, video_x11_window, 
   126 				0, 0, width, height, 0, visual->depth, 
   127 				InputOutput, visual->visual, 
   128 				CWColormap | CWEventMask, 
   129 				&win_attrs );
   130     if( glx_window == None ) {
   131 	/* Hrm. Aww, no window? */
   132 	ERROR( "Unable to create GLX window" );
   133 	glXDestroyContext( video_x11_display, glx_context );
   134 	if( win_attrs.colormap ) 
   135 	    XFreeColormap( video_x11_display, win_attrs.colormap );
   136 	return FALSE;
   137     }
   138     XMapRaised( video_x11_display, glx_window );
   140     /* And finally set the window to be the active drawing area */
   141     if( glXMakeCurrent( video_x11_display, glx_window, glx_context ) == False ) {
   142 	/* Ok you have _GOT_ to be kidding me */
   143 	ERROR( "Unable to prepare GLX window for drawing" );
   144 	XDestroyWindow( video_x11_display, glx_window );
   145 	XFreeColormap( video_x11_display, win_attrs.colormap );
   146 	glXDestroyContext( video_x11_display, glx_context );
   147 	return FALSE;
   148     }
   149     return TRUE;
   150 }
   152 void video_glx_shutdown()
   153 {
   154     if( glsl_loaded ) {
   155 	glsl_unload_shaders();
   156     }
   157     if( glx_window != None ) {
   158 	XDestroyWindow( video_x11_display, glx_window );
   159 	XFreeColormap( video_x11_display, win_attrs.colormap );
   160 	glx_window = None;
   161     }
   162     if( glx_context != NULL ) {
   163 	glXDestroyContext( video_x11_display, glx_context );
   164 	glx_context = NULL;
   165     }
   166 }
   169 int video_glx_load_font( const gchar *font_name )
   170 {
   171     int lists;
   172     XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
   173     if (font == NULL)
   174 	return -1;
   176     lists = glGenLists(96);
   177     glXUseXFont(font->fid, 32, 96, lists);
   178     XFreeFont(video_x11_display, font);
   179 }
   182 void video_glx_swap_buffers( void )
   183 {
   184     glXSwapBuffers( video_x11_display, glx_window );
   185 }
.