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