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 442:c0dcf22c8e08
prev439:f0c7928c5914
next478:57f73576c974
author nkeynes
date Sat Oct 13 04:00:23 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Cleanup window creation - not used now anyway
file annotate diff log raw
nkeynes@94
     1
/**
nkeynes@442
     2
 * $Id: video_x11.c,v 1.18 2007-10-13 04:00:23 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@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@442
    78
/**
nkeynes@442
    79
 * Create a new window with a custom visual - not used at the moment,
nkeynes@442
    80
 * but retained for future reference.
nkeynes@442
    81
 */
nkeynes@442
    82
gboolean video_x11_create_window( int width, int height )
nkeynes@103
    83
{
nkeynes@103
    84
    int visual_attrs[] = { GLX_RGBA, GLX_RED_SIZE, 4, 
nkeynes@103
    85
			   GLX_GREEN_SIZE, 4, 
nkeynes@103
    86
			   GLX_BLUE_SIZE, 4,
nkeynes@103
    87
			   GLX_ALPHA_SIZE, 4,
nkeynes@335
    88
			   GLX_DEPTH_SIZE, 24,
nkeynes@103
    89
			   GLX_DOUBLEBUFFER, 
nkeynes@103
    90
			   None };
nkeynes@103
    91
    int screen = XScreenNumberOfScreen(video_x11_screen);
nkeynes@103
    92
    XVisualInfo *visual;
nkeynes@442
    93
    /* Find ourselves a nice visual */
nkeynes@442
    94
    visual = glXChooseVisual( video_x11_display, 
nkeynes@442
    95
			      screen,
nkeynes@442
    96
			      visual_attrs );
nkeynes@103
    97
nkeynes@442
    98
    /* Create a child window with the visual in question */
nkeynes@103
    99
    win_attrs.event_mask = 0;
nkeynes@103
   100
    win_attrs.colormap = XCreateColormap( video_x11_display, 
nkeynes@103
   101
					  RootWindowOfScreen(video_x11_screen),
nkeynes@103
   102
					  visual->visual, AllocNone );
nkeynes@103
   103
    glx_window = XCreateWindow( video_x11_display, video_x11_window, 
nkeynes@352
   104
				0, 0, width, height, 0, visual->depth, 
nkeynes@103
   105
				InputOutput, visual->visual, 
nkeynes@103
   106
				CWColormap | CWEventMask, 
nkeynes@103
   107
				&win_attrs );
nkeynes@103
   108
    if( glx_window == None ) {
nkeynes@103
   109
	/* Hrm. Aww, no window? */
nkeynes@103
   110
	ERROR( "Unable to create GLX window" );
nkeynes@103
   111
	if( win_attrs.colormap ) 
nkeynes@103
   112
	    XFreeColormap( video_x11_display, win_attrs.colormap );
nkeynes@442
   113
	XFree(visual);
nkeynes@103
   114
	return FALSE;
nkeynes@103
   115
    }
nkeynes@103
   116
    XMapRaised( video_x11_display, glx_window );
nkeynes@439
   117
nkeynes@442
   118
    XFree(visual);
nkeynes@442
   119
    return TRUE;
nkeynes@442
   120
}
nkeynes@439
   121
nkeynes@442
   122
gboolean video_glx_init_context( Window window )
nkeynes@442
   123
{
nkeynes@442
   124
    XWindowAttributes attr;
nkeynes@442
   125
    XVisualInfo *visual;
nkeynes@442
   126
    XVisualInfo query;
nkeynes@442
   127
    int query_items = 1;
nkeynes@442
   128
nkeynes@442
   129
    XGetWindowAttributes(video_x11_display, window, &attr);
nkeynes@442
   130
    
nkeynes@442
   131
    query.visualid = XVisualIDFromVisual(attr.visual);
nkeynes@442
   132
    visual = XGetVisualInfo(video_x11_display, VisualIDMask, &query, &query_items );
nkeynes@442
   133
    if( visual == NULL ) {
nkeynes@442
   134
	ERROR( "Unable to obtain a compatible visual" );
nkeynes@103
   135
	return FALSE;
nkeynes@103
   136
    }
nkeynes@442
   137
nkeynes@442
   138
    int major, minor;
nkeynes@442
   139
    if( glXQueryVersion( video_x11_display, &major, &minor ) == False ) {
nkeynes@442
   140
	ERROR( "X Display lacks the GLX nature" );
nkeynes@442
   141
	XFree(visual);
nkeynes@442
   142
	return FALSE;
nkeynes@442
   143
    }
nkeynes@442
   144
    if( major < 1 || minor < 2 ) {
nkeynes@442
   145
	ERROR( "X display supports GLX %d.%d, but we need at least 1.2", major, minor );
nkeynes@442
   146
	XFree(visual);
nkeynes@442
   147
	return FALSE;
nkeynes@442
   148
    }
nkeynes@442
   149
nkeynes@442
   150
    /* And a matching gl context */
nkeynes@442
   151
    glx_context = glXCreateContext( video_x11_display, visual, None, True );
nkeynes@442
   152
    if( glx_context == NULL ) {
nkeynes@442
   153
	ERROR( "Unable to obtain a GLX Context. Possibly your system is broken in some small, undefineable way" );
nkeynes@442
   154
	XFree(visual);
nkeynes@442
   155
	return FALSE;
nkeynes@442
   156
    }
nkeynes@442
   157
nkeynes@442
   158
    if( glXMakeCurrent( video_x11_display, window, glx_context ) == False ) {
nkeynes@442
   159
	ERROR( "Unable to prepare GLX context for drawing" );
nkeynes@442
   160
	glXDestroyContext( video_x11_display, glx_context );
nkeynes@442
   161
	XFree(visual);
nkeynes@442
   162
	return FALSE;
nkeynes@442
   163
    }
nkeynes@442
   164
    XFree(visual);
nkeynes@103
   165
    return TRUE;
nkeynes@103
   166
}
nkeynes@370
   167
nkeynes@442
   168
nkeynes@352
   169
void video_glx_shutdown()
nkeynes@352
   170
{
nkeynes@405
   171
    if( glsl_loaded ) {
nkeynes@405
   172
	glsl_unload_shaders();
nkeynes@405
   173
    }
nkeynes@370
   174
    if( glx_window != None ) {
nkeynes@370
   175
	XDestroyWindow( video_x11_display, glx_window );
nkeynes@370
   176
	XFreeColormap( video_x11_display, win_attrs.colormap );
nkeynes@370
   177
	glx_window = None;
nkeynes@370
   178
    }
nkeynes@370
   179
    if( glx_context != NULL ) {
nkeynes@370
   180
	glXDestroyContext( video_x11_display, glx_context );
nkeynes@370
   181
	glx_context = NULL;
nkeynes@370
   182
    }
nkeynes@352
   183
}
nkeynes@352
   184
nkeynes@103
   185
nkeynes@108
   186
int video_glx_load_font( const gchar *font_name )
nkeynes@108
   187
{
nkeynes@108
   188
    int lists;
nkeynes@108
   189
    XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
nkeynes@108
   190
    if (font == NULL)
nkeynes@108
   191
	return -1;
nkeynes@108
   192
    
nkeynes@108
   193
    lists = glGenLists(96);
nkeynes@108
   194
    glXUseXFont(font->fid, 32, 96, lists);
nkeynes@108
   195
    XFreeFont(video_x11_display, font);
nkeynes@424
   196
    return lists;
nkeynes@108
   197
}
nkeynes@108
   198
nkeynes@108
   199
nkeynes@103
   200
void video_glx_swap_buffers( void )
nkeynes@103
   201
{
nkeynes@103
   202
    glXSwapBuffers( video_x11_display, glx_window );
nkeynes@103
   203
}
nkeynes@103
   204
.