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