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
file annotate diff log raw
nkeynes@94
     1
/**
nkeynes@478
     2
 * $Id: video_x11.c,v 1.19 2007-10-31 09:11:14 nkeynes Exp $
nkeynes@94
     3
 *
nkeynes@103
     4
 * Shared functions for all X11-based display drivers.
nkeynes@94
     5
 *
nkeynes@94
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@94
     7
 *
nkeynes@94
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@94
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@94
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@94
    11
 * (at your option) any later version.
nkeynes@94
    12
 *
nkeynes@94
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@94
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@94
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@94
    16
 * GNU General Public License for more details.
nkeynes@94
    17
 */
nkeynes@94
    18
nkeynes@103
    19
#include <X11/Xlib.h>
nkeynes@103
    20
#include <GL/glx.h>
nkeynes@103
    21
#include "dream.h"
nkeynes@94
    22
#include "drivers/video_x11.h"
nkeynes@405
    23
#include "drivers/gl_common.h"
nkeynes@94
    24
nkeynes@352
    25
extern uint32_t video_width, video_height;
nkeynes@352
    26
nkeynes@103
    27
/**
nkeynes@103
    28
 * General X11 parameters. The front-end driver is expected to set this up
nkeynes@352
    29
 * by calling video_glx_init after initializing itself.
nkeynes@103
    30
 */
nkeynes@352
    31
static Display *video_x11_display = NULL;
nkeynes@352
    32
static Screen *video_x11_screen = NULL;
nkeynes@352
    33
static Window video_x11_window = 0;
nkeynes@405
    34
static gboolean glsl_loaded = FALSE;
nkeynes@352
    35
nkeynes@103
    36
/**
nkeynes@103
    37
 * GLX parameters.
nkeynes@103
    38
 */
nkeynes@352
    39
static GLXContext glx_context;
nkeynes@352
    40
static Window glx_window;
nkeynes@352
    41
static XSetWindowAttributes win_attrs;
nkeynes@103
    42
nkeynes@424
    43
gboolean video_glx_create_window( int width, int height );
nkeynes@424
    44
nkeynes@352
    45
gboolean video_glx_init( Display *display, Screen *screen, Window window,
nkeynes@352
    46
			 int width, int height, display_driver_t driver )
nkeynes@94
    47
{
nkeynes@94
    48
    video_x11_display = display;
nkeynes@94
    49
    video_x11_screen = screen;
nkeynes@442
    50
    glx_window = video_x11_window = window;
nkeynes@352
    51
nkeynes@442
    52
    if( !video_glx_init_context(glx_window) ) {
nkeynes@352
    53
	return FALSE;
nkeynes@352
    54
    }
nkeynes@352
    55
nkeynes@478
    56
    if( !glXIsDirect(video_x11_display, glx_context) ) {
nkeynes@478
    57
    	WARN( "Not using direct rendering - this is likely to be slow" );
nkeynes@478
    58
    }
nkeynes@478
    59
nkeynes@352
    60
    if( gl_fbo_is_supported() ) {
nkeynes@352
    61
	gl_fbo_init(driver);
nkeynes@405
    62
nkeynes@405
    63
#ifdef USE_GLSL
nkeynes@405
    64
	if( glsl_is_supported() ) {
nkeynes@405
    65
	    glsl_loaded = glsl_load_shaders( glsl_vertex_shader_src, glsl_fragment_shader_src );
nkeynes@405
    66
	    if( !glsl_loaded ) {
nkeynes@405
    67
	        WARN( "Shaders failed to load" );
nkeynes@405
    68
	    }
nkeynes@405
    69
	} else {
nkeynes@405
    70
	    WARN( "Shaders not supported" );
nkeynes@405
    71
	}
nkeynes@405
    72
#endif
nkeynes@352
    73
	return TRUE;
nkeynes@352
    74
    } else {
nkeynes@352
    75
	/* Pbuffers? */
nkeynes@352
    76
	ERROR( "Framebuffer objects not supported (required in this version)" );
nkeynes@352
    77
	video_glx_shutdown();
nkeynes@352
    78
	return FALSE;
nkeynes@352
    79
    }
nkeynes@94
    80
}
nkeynes@94
    81
nkeynes@442
    82
/**
nkeynes@442
    83
 * Create a new window with a custom visual - not used at the moment,
nkeynes@442
    84
 * but retained for future reference.
nkeynes@442
    85
 */
nkeynes@442
    86
gboolean video_x11_create_window( int width, int height )
nkeynes@103
    87
{
nkeynes@103
    88
    int visual_attrs[] = { GLX_RGBA, GLX_RED_SIZE, 4, 
nkeynes@103
    89
			   GLX_GREEN_SIZE, 4, 
nkeynes@103
    90
			   GLX_BLUE_SIZE, 4,
nkeynes@103
    91
			   GLX_ALPHA_SIZE, 4,
nkeynes@335
    92
			   GLX_DEPTH_SIZE, 24,
nkeynes@103
    93
			   GLX_DOUBLEBUFFER, 
nkeynes@103
    94
			   None };
nkeynes@103
    95
    int screen = XScreenNumberOfScreen(video_x11_screen);
nkeynes@103
    96
    XVisualInfo *visual;
nkeynes@442
    97
    /* Find ourselves a nice visual */
nkeynes@442
    98
    visual = glXChooseVisual( video_x11_display, 
nkeynes@442
    99
			      screen,
nkeynes@442
   100
			      visual_attrs );
nkeynes@103
   101
nkeynes@442
   102
    /* Create a child window with the visual in question */
nkeynes@103
   103
    win_attrs.event_mask = 0;
nkeynes@103
   104
    win_attrs.colormap = XCreateColormap( video_x11_display, 
nkeynes@103
   105
					  RootWindowOfScreen(video_x11_screen),
nkeynes@103
   106
					  visual->visual, AllocNone );
nkeynes@103
   107
    glx_window = XCreateWindow( video_x11_display, video_x11_window, 
nkeynes@352
   108
				0, 0, width, height, 0, visual->depth, 
nkeynes@103
   109
				InputOutput, visual->visual, 
nkeynes@103
   110
				CWColormap | CWEventMask, 
nkeynes@103
   111
				&win_attrs );
nkeynes@103
   112
    if( glx_window == None ) {
nkeynes@103
   113
	/* Hrm. Aww, no window? */
nkeynes@103
   114
	ERROR( "Unable to create GLX window" );
nkeynes@103
   115
	if( win_attrs.colormap ) 
nkeynes@103
   116
	    XFreeColormap( video_x11_display, win_attrs.colormap );
nkeynes@442
   117
	XFree(visual);
nkeynes@103
   118
	return FALSE;
nkeynes@103
   119
    }
nkeynes@103
   120
    XMapRaised( video_x11_display, glx_window );
nkeynes@439
   121
nkeynes@442
   122
    XFree(visual);
nkeynes@442
   123
    return TRUE;
nkeynes@442
   124
}
nkeynes@439
   125
nkeynes@442
   126
gboolean video_glx_init_context( Window window )
nkeynes@442
   127
{
nkeynes@442
   128
    XWindowAttributes attr;
nkeynes@442
   129
    XVisualInfo *visual;
nkeynes@442
   130
    XVisualInfo query;
nkeynes@442
   131
    int query_items = 1;
nkeynes@442
   132
nkeynes@442
   133
    XGetWindowAttributes(video_x11_display, window, &attr);
nkeynes@442
   134
    
nkeynes@442
   135
    query.visualid = XVisualIDFromVisual(attr.visual);
nkeynes@442
   136
    visual = XGetVisualInfo(video_x11_display, VisualIDMask, &query, &query_items );
nkeynes@442
   137
    if( visual == NULL ) {
nkeynes@442
   138
	ERROR( "Unable to obtain a compatible visual" );
nkeynes@103
   139
	return FALSE;
nkeynes@103
   140
    }
nkeynes@442
   141
nkeynes@442
   142
    int major, minor;
nkeynes@442
   143
    if( glXQueryVersion( video_x11_display, &major, &minor ) == False ) {
nkeynes@442
   144
	ERROR( "X Display lacks the GLX nature" );
nkeynes@442
   145
	XFree(visual);
nkeynes@442
   146
	return FALSE;
nkeynes@442
   147
    }
nkeynes@442
   148
    if( major < 1 || minor < 2 ) {
nkeynes@442
   149
	ERROR( "X display supports GLX %d.%d, but we need at least 1.2", major, minor );
nkeynes@442
   150
	XFree(visual);
nkeynes@442
   151
	return FALSE;
nkeynes@442
   152
    }
nkeynes@442
   153
nkeynes@442
   154
    /* And a matching gl context */
nkeynes@442
   155
    glx_context = glXCreateContext( video_x11_display, visual, None, True );
nkeynes@442
   156
    if( glx_context == NULL ) {
nkeynes@442
   157
	ERROR( "Unable to obtain a GLX Context. Possibly your system is broken in some small, undefineable way" );
nkeynes@442
   158
	XFree(visual);
nkeynes@442
   159
	return FALSE;
nkeynes@442
   160
    }
nkeynes@442
   161
nkeynes@442
   162
    if( glXMakeCurrent( video_x11_display, window, glx_context ) == False ) {
nkeynes@442
   163
	ERROR( "Unable to prepare GLX context for drawing" );
nkeynes@442
   164
	glXDestroyContext( video_x11_display, glx_context );
nkeynes@442
   165
	XFree(visual);
nkeynes@442
   166
	return FALSE;
nkeynes@442
   167
    }
nkeynes@442
   168
    XFree(visual);
nkeynes@103
   169
    return TRUE;
nkeynes@103
   170
}
nkeynes@370
   171
nkeynes@442
   172
nkeynes@352
   173
void video_glx_shutdown()
nkeynes@352
   174
{
nkeynes@405
   175
    if( glsl_loaded ) {
nkeynes@405
   176
	glsl_unload_shaders();
nkeynes@405
   177
    }
nkeynes@370
   178
    if( glx_window != None ) {
nkeynes@370
   179
	XDestroyWindow( video_x11_display, glx_window );
nkeynes@370
   180
	XFreeColormap( video_x11_display, win_attrs.colormap );
nkeynes@370
   181
	glx_window = None;
nkeynes@370
   182
    }
nkeynes@370
   183
    if( glx_context != NULL ) {
nkeynes@370
   184
	glXDestroyContext( video_x11_display, glx_context );
nkeynes@370
   185
	glx_context = NULL;
nkeynes@370
   186
    }
nkeynes@352
   187
}
nkeynes@352
   188
nkeynes@103
   189
nkeynes@108
   190
int video_glx_load_font( const gchar *font_name )
nkeynes@108
   191
{
nkeynes@108
   192
    int lists;
nkeynes@108
   193
    XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
nkeynes@108
   194
    if (font == NULL)
nkeynes@108
   195
	return -1;
nkeynes@108
   196
    
nkeynes@108
   197
    lists = glGenLists(96);
nkeynes@108
   198
    glXUseXFont(font->fid, 32, 96, lists);
nkeynes@108
   199
    XFreeFont(video_x11_display, font);
nkeynes@424
   200
    return lists;
nkeynes@108
   201
}
nkeynes@108
   202
nkeynes@108
   203
nkeynes@103
   204
void video_glx_swap_buffers( void )
nkeynes@103
   205
{
nkeynes@103
   206
    glXSwapBuffers( video_x11_display, glx_window );
nkeynes@103
   207
}
nkeynes@103
   208
.