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 103:9b9cfc5855e0
prev94:8d80d9c7cc7d
next108:565de331ccec
author nkeynes
date Mon Mar 13 12:39:07 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change More rendering work in progress. Almost there now...
file annotate diff log raw
nkeynes@94
     1
/**
nkeynes@103
     2
 * $Id: video_x11.c,v 1.2 2006-03-13 12:39:07 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@103
    22
#include "video.h"
nkeynes@94
    23
#include "drivers/video_x11.h"
nkeynes@94
    24
nkeynes@103
    25
/**
nkeynes@103
    26
 * General X11 parameters. The front-end driver is expected to set this up
nkeynes@103
    27
 * by calling video_x11_set_display after initializing itself.
nkeynes@103
    28
 */
nkeynes@94
    29
Display *video_x11_display = NULL;
nkeynes@94
    30
Screen *video_x11_screen = NULL;
nkeynes@94
    31
Window video_x11_window = 0;
nkeynes@94
    32
nkeynes@103
    33
/**
nkeynes@103
    34
 * GLX parameters.
nkeynes@103
    35
 */
nkeynes@103
    36
GLXContext glx_context;
nkeynes@103
    37
Window glx_window;
nkeynes@103
    38
gboolean glx_open = FALSE;
nkeynes@103
    39
nkeynes@94
    40
void video_x11_set_display( Display *display, Screen *screen, Window window )
nkeynes@94
    41
{
nkeynes@94
    42
    video_x11_display = display;
nkeynes@94
    43
    video_x11_screen = screen;
nkeynes@94
    44
    video_x11_window = window;
nkeynes@94
    45
}
nkeynes@94
    46
nkeynes@103
    47
nkeynes@103
    48
gboolean video_glx_create_window( int x, int y, int width, int height )
nkeynes@103
    49
{
nkeynes@103
    50
    int major, minor;
nkeynes@103
    51
    const char *glxExts, *glxServer;
nkeynes@103
    52
    int visual_attrs[] = { GLX_RGBA, GLX_RED_SIZE, 4, 
nkeynes@103
    53
			   GLX_GREEN_SIZE, 4, 
nkeynes@103
    54
			   GLX_BLUE_SIZE, 4,
nkeynes@103
    55
			   GLX_ALPHA_SIZE, 4,
nkeynes@103
    56
			   GLX_DEPTH_SIZE, 16,
nkeynes@103
    57
			   GLX_DOUBLEBUFFER, 
nkeynes@103
    58
			   None };
nkeynes@103
    59
    int screen = XScreenNumberOfScreen(video_x11_screen);
nkeynes@103
    60
    XSetWindowAttributes win_attrs;
nkeynes@103
    61
    XVisualInfo *visual;
nkeynes@103
    62
nkeynes@103
    63
    if( glXQueryVersion( video_x11_display, &major, &minor ) == False ) {
nkeynes@103
    64
	ERROR( "X Display lacks the GLX nature" );
nkeynes@103
    65
	return FALSE;
nkeynes@103
    66
    }
nkeynes@103
    67
    if( major < 1 || minor < 2 ) {
nkeynes@103
    68
	ERROR( "X display supports GLX %d.%d, but we need at least 1.2", major, minor );
nkeynes@103
    69
	return FALSE;
nkeynes@103
    70
    }
nkeynes@103
    71
nkeynes@103
    72
    glxExts = glXQueryExtensionsString( video_x11_display, screen );
nkeynes@103
    73
    glxServer = glXQueryServerString( video_x11_display, screen, GLX_VENDOR );
nkeynes@103
    74
    INFO( "GLX version %d.%d, %s. Supported exts: %s", major, minor,
nkeynes@103
    75
	  glxServer, glxExts );
nkeynes@103
    76
nkeynes@103
    77
    /* Find ourselves a nice visual */
nkeynes@103
    78
    visual = glXChooseVisual( video_x11_display, 
nkeynes@103
    79
			      screen,
nkeynes@103
    80
			      visual_attrs );
nkeynes@103
    81
    if( visual == NULL ) {
nkeynes@103
    82
	ERROR( "Unable to obtain a compatible visual" );
nkeynes@103
    83
	return FALSE;
nkeynes@103
    84
    }
nkeynes@103
    85
nkeynes@103
    86
    /* And a matching gl context */
nkeynes@103
    87
    glx_context = glXCreateContext( video_x11_display, visual, None, True );
nkeynes@103
    88
    if( glx_context == NULL ) {
nkeynes@103
    89
	ERROR( "Unable to obtain a GLX Context. Possibly your system is broken in some small, undefineable way" );
nkeynes@103
    90
	return FALSE;
nkeynes@103
    91
    }
nkeynes@103
    92
    
nkeynes@103
    93
nkeynes@103
    94
    /* Ok, all good so far. Unfortunately the visual we need to use will 
nkeynes@103
    95
     * almost certainly be different from the one our frame is using. Which 
nkeynes@103
    96
     * means we have to jump through the following hoops to create a 
nkeynes@103
    97
     * child window with the appropriate settings.
nkeynes@103
    98
     */
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@103
   104
				x, y, 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
	glXDestroyContext( video_x11_display, glx_context );
nkeynes@103
   112
	if( win_attrs.colormap ) 
nkeynes@103
   113
	    XFreeColormap( video_x11_display, win_attrs.colormap );
nkeynes@103
   114
	return FALSE;
nkeynes@103
   115
    }
nkeynes@103
   116
    XMapRaised( video_x11_display, glx_window );
nkeynes@103
   117
nkeynes@103
   118
    /* And finally set the window to be the active drawing area */
nkeynes@103
   119
    if( glXMakeCurrent( video_x11_display, glx_window, glx_context ) == False ) {
nkeynes@103
   120
	/* Ok you have _GOT_ to be kidding me */
nkeynes@103
   121
	ERROR( "Unable to prepare GLX window for drawing" );
nkeynes@103
   122
	XDestroyWindow( video_x11_display, glx_window );
nkeynes@103
   123
	XFreeColormap( video_x11_display, win_attrs.colormap );
nkeynes@103
   124
	glXDestroyContext( video_x11_display, glx_context );
nkeynes@103
   125
	return FALSE;
nkeynes@103
   126
    }
nkeynes@103
   127
    
nkeynes@103
   128
    glx_open = TRUE;
nkeynes@103
   129
    return TRUE;
nkeynes@103
   130
}
nkeynes@103
   131
nkeynes@103
   132
void video_glx_swap_buffers( void )
nkeynes@103
   133
{
nkeynes@103
   134
    glXSwapBuffers( video_x11_display, glx_window );
nkeynes@103
   135
}
nkeynes@103
   136
nkeynes@103
   137
void video_glx_create_pixmap( int width, int height )
nkeynes@103
   138
{
nkeynes@103
   139
nkeynes@103
   140
}
.