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 478:57f73576c974
prev442:c0dcf22c8e08
next481:3b2d6c5a19ad
author nkeynes
date Wed Oct 31 09:11:14 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Add warning for non-direct rendering
view annotate diff log raw
     1 /**
     2  * $Id: video_x11.c,v 1.19 2007-10-31 09:11: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_create_window( int width, int height );
    45 gboolean video_glx_init( Display *display, Screen *screen, Window window,
    46 			 int width, int height, display_driver_t driver )
    47 {
    48     video_x11_display = display;
    49     video_x11_screen = screen;
    50     glx_window = video_x11_window = window;
    52     if( !video_glx_init_context(glx_window) ) {
    53 	return FALSE;
    54     }
    56     if( !glXIsDirect(video_x11_display, glx_context) ) {
    57     	WARN( "Not using direct rendering - this is likely to be slow" );
    58     }
    60     if( gl_fbo_is_supported() ) {
    61 	gl_fbo_init(driver);
    63 #ifdef USE_GLSL
    64 	if( glsl_is_supported() ) {
    65 	    glsl_loaded = glsl_load_shaders( glsl_vertex_shader_src, glsl_fragment_shader_src );
    66 	    if( !glsl_loaded ) {
    67 	        WARN( "Shaders failed to load" );
    68 	    }
    69 	} else {
    70 	    WARN( "Shaders not supported" );
    71 	}
    72 #endif
    73 	return TRUE;
    74     } else {
    75 	/* Pbuffers? */
    76 	ERROR( "Framebuffer objects not supported (required in this version)" );
    77 	video_glx_shutdown();
    78 	return FALSE;
    79     }
    80 }
    82 /**
    83  * Create a new window with a custom visual - not used at the moment,
    84  * but retained for future reference.
    85  */
    86 gboolean video_x11_create_window( int width, int height )
    87 {
    88     int visual_attrs[] = { GLX_RGBA, GLX_RED_SIZE, 4, 
    89 			   GLX_GREEN_SIZE, 4, 
    90 			   GLX_BLUE_SIZE, 4,
    91 			   GLX_ALPHA_SIZE, 4,
    92 			   GLX_DEPTH_SIZE, 24,
    93 			   GLX_DOUBLEBUFFER, 
    94 			   None };
    95     int screen = XScreenNumberOfScreen(video_x11_screen);
    96     XVisualInfo *visual;
    97     /* Find ourselves a nice visual */
    98     visual = glXChooseVisual( video_x11_display, 
    99 			      screen,
   100 			      visual_attrs );
   102     /* Create a child window with the visual in question */
   103     win_attrs.event_mask = 0;
   104     win_attrs.colormap = XCreateColormap( video_x11_display, 
   105 					  RootWindowOfScreen(video_x11_screen),
   106 					  visual->visual, AllocNone );
   107     glx_window = XCreateWindow( video_x11_display, video_x11_window, 
   108 				0, 0, width, height, 0, visual->depth, 
   109 				InputOutput, visual->visual, 
   110 				CWColormap | CWEventMask, 
   111 				&win_attrs );
   112     if( glx_window == None ) {
   113 	/* Hrm. Aww, no window? */
   114 	ERROR( "Unable to create GLX window" );
   115 	if( win_attrs.colormap ) 
   116 	    XFreeColormap( video_x11_display, win_attrs.colormap );
   117 	XFree(visual);
   118 	return FALSE;
   119     }
   120     XMapRaised( video_x11_display, glx_window );
   122     XFree(visual);
   123     return TRUE;
   124 }
   126 gboolean video_glx_init_context( Window window )
   127 {
   128     XWindowAttributes attr;
   129     XVisualInfo *visual;
   130     XVisualInfo query;
   131     int query_items = 1;
   133     XGetWindowAttributes(video_x11_display, window, &attr);
   135     query.visualid = XVisualIDFromVisual(attr.visual);
   136     visual = XGetVisualInfo(video_x11_display, VisualIDMask, &query, &query_items );
   137     if( visual == NULL ) {
   138 	ERROR( "Unable to obtain a compatible visual" );
   139 	return FALSE;
   140     }
   142     int major, minor;
   143     if( glXQueryVersion( video_x11_display, &major, &minor ) == False ) {
   144 	ERROR( "X Display lacks the GLX nature" );
   145 	XFree(visual);
   146 	return FALSE;
   147     }
   148     if( major < 1 || minor < 2 ) {
   149 	ERROR( "X display supports GLX %d.%d, but we need at least 1.2", major, minor );
   150 	XFree(visual);
   151 	return FALSE;
   152     }
   154     /* And a matching gl context */
   155     glx_context = glXCreateContext( video_x11_display, visual, None, True );
   156     if( glx_context == NULL ) {
   157 	ERROR( "Unable to obtain a GLX Context. Possibly your system is broken in some small, undefineable way" );
   158 	XFree(visual);
   159 	return FALSE;
   160     }
   162     if( glXMakeCurrent( video_x11_display, window, glx_context ) == False ) {
   163 	ERROR( "Unable to prepare GLX context for drawing" );
   164 	glXDestroyContext( video_x11_display, glx_context );
   165 	XFree(visual);
   166 	return FALSE;
   167     }
   168     XFree(visual);
   169     return TRUE;
   170 }
   173 void video_glx_shutdown()
   174 {
   175     if( glsl_loaded ) {
   176 	glsl_unload_shaders();
   177     }
   178     if( glx_window != None ) {
   179 	XDestroyWindow( video_x11_display, glx_window );
   180 	XFreeColormap( video_x11_display, win_attrs.colormap );
   181 	glx_window = None;
   182     }
   183     if( glx_context != NULL ) {
   184 	glXDestroyContext( video_x11_display, glx_context );
   185 	glx_context = NULL;
   186     }
   187 }
   190 int video_glx_load_font( const gchar *font_name )
   191 {
   192     int lists;
   193     XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
   194     if (font == NULL)
   195 	return -1;
   197     lists = glGenLists(96);
   198     glXUseXFont(font->fid, 32, 96, lists);
   199     XFreeFont(video_x11_display, font);
   200     return lists;
   201 }
   204 void video_glx_swap_buffers( void )
   205 {
   206     glXSwapBuffers( video_x11_display, glx_window );
   207 }
.