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
file annotate diff log raw
nkeynes@94
     1
/**
nkeynes@405
     2
 * $Id: video_x11.c,v 1.14 2007-09-28 07:24: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@352
    43
gboolean video_glx_init( Display *display, Screen *screen, Window window,
nkeynes@352
    44
			 int width, int height, display_driver_t driver )
nkeynes@94
    45
{
nkeynes@94
    46
    video_x11_display = display;
nkeynes@94
    47
    video_x11_screen = screen;
nkeynes@94
    48
    video_x11_window = window;
nkeynes@352
    49
nkeynes@352
    50
    if( !video_glx_create_window(width,height) ) {
nkeynes@352
    51
	return FALSE;
nkeynes@352
    52
    }
nkeynes@352
    53
nkeynes@352
    54
    if( gl_fbo_is_supported() ) {
nkeynes@352
    55
	gl_fbo_init(driver);
nkeynes@405
    56
nkeynes@405
    57
#ifdef USE_GLSL
nkeynes@405
    58
	if( glsl_is_supported() ) {
nkeynes@405
    59
	    glsl_loaded = glsl_load_shaders( glsl_vertex_shader_src, glsl_fragment_shader_src );
nkeynes@405
    60
	    if( !glsl_loaded ) {
nkeynes@405
    61
	        WARN( "Shaders failed to load" );
nkeynes@405
    62
	    }
nkeynes@405
    63
	} else {
nkeynes@405
    64
	    WARN( "Shaders not supported" );
nkeynes@405
    65
	}
nkeynes@405
    66
#endif
nkeynes@352
    67
	return TRUE;
nkeynes@352
    68
    } else {
nkeynes@352
    69
	/* Pbuffers? */
nkeynes@352
    70
	ERROR( "Framebuffer objects not supported (required in this version)" );
nkeynes@352
    71
	video_glx_shutdown();
nkeynes@352
    72
	return FALSE;
nkeynes@352
    73
    }
nkeynes@94
    74
}
nkeynes@94
    75
nkeynes@352
    76
gboolean video_glx_create_window( int width, int height )
nkeynes@103
    77
{
nkeynes@103
    78
    int major, minor;
nkeynes@103
    79
    const char *glxExts, *glxServer;
nkeynes@103
    80
    int visual_attrs[] = { GLX_RGBA, GLX_RED_SIZE, 4, 
nkeynes@103
    81
			   GLX_GREEN_SIZE, 4, 
nkeynes@103
    82
			   GLX_BLUE_SIZE, 4,
nkeynes@103
    83
			   GLX_ALPHA_SIZE, 4,
nkeynes@335
    84
			   GLX_DEPTH_SIZE, 24,
nkeynes@103
    85
			   GLX_DOUBLEBUFFER, 
nkeynes@103
    86
			   None };
nkeynes@103
    87
    int screen = XScreenNumberOfScreen(video_x11_screen);
nkeynes@103
    88
    XVisualInfo *visual;
nkeynes@103
    89
nkeynes@103
    90
    if( glXQueryVersion( video_x11_display, &major, &minor ) == False ) {
nkeynes@103
    91
	ERROR( "X Display lacks the GLX nature" );
nkeynes@103
    92
	return FALSE;
nkeynes@103
    93
    }
nkeynes@103
    94
    if( major < 1 || minor < 2 ) {
nkeynes@103
    95
	ERROR( "X display supports GLX %d.%d, but we need at least 1.2", major, minor );
nkeynes@103
    96
	return FALSE;
nkeynes@103
    97
    }
nkeynes@103
    98
nkeynes@103
    99
    /* Find ourselves a nice visual */
nkeynes@103
   100
    visual = glXChooseVisual( video_x11_display, 
nkeynes@103
   101
			      screen,
nkeynes@103
   102
			      visual_attrs );
nkeynes@103
   103
    if( visual == NULL ) {
nkeynes@103
   104
	ERROR( "Unable to obtain a compatible visual" );
nkeynes@103
   105
	return FALSE;
nkeynes@103
   106
    }
nkeynes@103
   107
nkeynes@103
   108
    /* And a matching gl context */
nkeynes@103
   109
    glx_context = glXCreateContext( video_x11_display, visual, None, True );
nkeynes@103
   110
    if( glx_context == NULL ) {
nkeynes@103
   111
	ERROR( "Unable to obtain a GLX Context. Possibly your system is broken in some small, undefineable way" );
nkeynes@103
   112
	return FALSE;
nkeynes@103
   113
    }
nkeynes@103
   114
    
nkeynes@103
   115
nkeynes@103
   116
    /* Ok, all good so far. Unfortunately the visual we need to use will 
nkeynes@103
   117
     * almost certainly be different from the one our frame is using. Which 
nkeynes@103
   118
     * means we have to jump through the following hoops to create a 
nkeynes@103
   119
     * child window with the appropriate settings.
nkeynes@103
   120
     */
nkeynes@103
   121
    win_attrs.event_mask = 0;
nkeynes@103
   122
    win_attrs.colormap = XCreateColormap( video_x11_display, 
nkeynes@103
   123
					  RootWindowOfScreen(video_x11_screen),
nkeynes@103
   124
					  visual->visual, AllocNone );
nkeynes@103
   125
    glx_window = XCreateWindow( video_x11_display, video_x11_window, 
nkeynes@352
   126
				0, 0, width, height, 0, visual->depth, 
nkeynes@103
   127
				InputOutput, visual->visual, 
nkeynes@103
   128
				CWColormap | CWEventMask, 
nkeynes@103
   129
				&win_attrs );
nkeynes@103
   130
    if( glx_window == None ) {
nkeynes@103
   131
	/* Hrm. Aww, no window? */
nkeynes@103
   132
	ERROR( "Unable to create GLX window" );
nkeynes@103
   133
	glXDestroyContext( video_x11_display, glx_context );
nkeynes@103
   134
	if( win_attrs.colormap ) 
nkeynes@103
   135
	    XFreeColormap( video_x11_display, win_attrs.colormap );
nkeynes@103
   136
	return FALSE;
nkeynes@103
   137
    }
nkeynes@103
   138
    XMapRaised( video_x11_display, glx_window );
nkeynes@103
   139
nkeynes@103
   140
    /* And finally set the window to be the active drawing area */
nkeynes@103
   141
    if( glXMakeCurrent( video_x11_display, glx_window, glx_context ) == False ) {
nkeynes@103
   142
	/* Ok you have _GOT_ to be kidding me */
nkeynes@103
   143
	ERROR( "Unable to prepare GLX window for drawing" );
nkeynes@103
   144
	XDestroyWindow( video_x11_display, glx_window );
nkeynes@103
   145
	XFreeColormap( video_x11_display, win_attrs.colormap );
nkeynes@103
   146
	glXDestroyContext( video_x11_display, glx_context );
nkeynes@103
   147
	return FALSE;
nkeynes@103
   148
    }
nkeynes@103
   149
    return TRUE;
nkeynes@103
   150
}
nkeynes@370
   151
nkeynes@352
   152
void video_glx_shutdown()
nkeynes@352
   153
{
nkeynes@405
   154
    if( glsl_loaded ) {
nkeynes@405
   155
	glsl_unload_shaders();
nkeynes@405
   156
    }
nkeynes@370
   157
    if( glx_window != None ) {
nkeynes@370
   158
	XDestroyWindow( video_x11_display, glx_window );
nkeynes@370
   159
	XFreeColormap( video_x11_display, win_attrs.colormap );
nkeynes@370
   160
	glx_window = None;
nkeynes@370
   161
    }
nkeynes@370
   162
    if( glx_context != NULL ) {
nkeynes@370
   163
	glXDestroyContext( video_x11_display, glx_context );
nkeynes@370
   164
	glx_context = NULL;
nkeynes@370
   165
    }
nkeynes@352
   166
}
nkeynes@352
   167
nkeynes@103
   168
nkeynes@108
   169
int video_glx_load_font( const gchar *font_name )
nkeynes@108
   170
{
nkeynes@108
   171
    int lists;
nkeynes@108
   172
    XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
nkeynes@108
   173
    if (font == NULL)
nkeynes@108
   174
	return -1;
nkeynes@108
   175
    
nkeynes@108
   176
    lists = glGenLists(96);
nkeynes@108
   177
    glXUseXFont(font->fid, 32, 96, lists);
nkeynes@108
   178
    XFreeFont(video_x11_display, font);
nkeynes@108
   179
}
nkeynes@108
   180
nkeynes@108
   181
nkeynes@103
   182
void video_glx_swap_buffers( void )
nkeynes@103
   183
{
nkeynes@103
   184
    glXSwapBuffers( video_x11_display, glx_window );
nkeynes@103
   185
}
nkeynes@103
   186
.