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...
view annotate diff log raw
     1 /**
     2  * $Id: video_x11.c,v 1.2 2006-03-13 12:39:07 nkeynes Exp $
     3  *
     4  * Shared functions for all X11-based display drivers.
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include <X11/Xlib.h>
    20 #include <GL/glx.h>
    21 #include "dream.h"
    22 #include "video.h"
    23 #include "drivers/video_x11.h"
    25 /**
    26  * General X11 parameters. The front-end driver is expected to set this up
    27  * by calling video_x11_set_display after initializing itself.
    28  */
    29 Display *video_x11_display = NULL;
    30 Screen *video_x11_screen = NULL;
    31 Window video_x11_window = 0;
    33 /**
    34  * GLX parameters.
    35  */
    36 GLXContext glx_context;
    37 Window glx_window;
    38 gboolean glx_open = FALSE;
    40 void video_x11_set_display( Display *display, Screen *screen, Window window )
    41 {
    42     video_x11_display = display;
    43     video_x11_screen = screen;
    44     video_x11_window = window;
    45 }
    48 gboolean video_glx_create_window( int x, int y, int width, int height )
    49 {
    50     int major, minor;
    51     const char *glxExts, *glxServer;
    52     int visual_attrs[] = { GLX_RGBA, GLX_RED_SIZE, 4, 
    53 			   GLX_GREEN_SIZE, 4, 
    54 			   GLX_BLUE_SIZE, 4,
    55 			   GLX_ALPHA_SIZE, 4,
    56 			   GLX_DEPTH_SIZE, 16,
    57 			   GLX_DOUBLEBUFFER, 
    58 			   None };
    59     int screen = XScreenNumberOfScreen(video_x11_screen);
    60     XSetWindowAttributes win_attrs;
    61     XVisualInfo *visual;
    63     if( glXQueryVersion( video_x11_display, &major, &minor ) == False ) {
    64 	ERROR( "X Display lacks the GLX nature" );
    65 	return FALSE;
    66     }
    67     if( major < 1 || minor < 2 ) {
    68 	ERROR( "X display supports GLX %d.%d, but we need at least 1.2", major, minor );
    69 	return FALSE;
    70     }
    72     glxExts = glXQueryExtensionsString( video_x11_display, screen );
    73     glxServer = glXQueryServerString( video_x11_display, screen, GLX_VENDOR );
    74     INFO( "GLX version %d.%d, %s. Supported exts: %s", major, minor,
    75 	  glxServer, glxExts );
    77     /* Find ourselves a nice visual */
    78     visual = glXChooseVisual( video_x11_display, 
    79 			      screen,
    80 			      visual_attrs );
    81     if( visual == NULL ) {
    82 	ERROR( "Unable to obtain a compatible visual" );
    83 	return FALSE;
    84     }
    86     /* And a matching gl context */
    87     glx_context = glXCreateContext( video_x11_display, visual, None, True );
    88     if( glx_context == NULL ) {
    89 	ERROR( "Unable to obtain a GLX Context. Possibly your system is broken in some small, undefineable way" );
    90 	return FALSE;
    91     }
    94     /* Ok, all good so far. Unfortunately the visual we need to use will 
    95      * almost certainly be different from the one our frame is using. Which 
    96      * means we have to jump through the following hoops to create a 
    97      * child window with the appropriate settings.
    98      */
    99     win_attrs.event_mask = 0;
   100     win_attrs.colormap = XCreateColormap( video_x11_display, 
   101 					  RootWindowOfScreen(video_x11_screen),
   102 					  visual->visual, AllocNone );
   103     glx_window = XCreateWindow( video_x11_display, video_x11_window, 
   104 				x, y, width, height, 0, visual->depth, 
   105 				InputOutput, visual->visual, 
   106 				CWColormap | CWEventMask, 
   107 				&win_attrs );
   108     if( glx_window == None ) {
   109 	/* Hrm. Aww, no window? */
   110 	ERROR( "Unable to create GLX window" );
   111 	glXDestroyContext( video_x11_display, glx_context );
   112 	if( win_attrs.colormap ) 
   113 	    XFreeColormap( video_x11_display, win_attrs.colormap );
   114 	return FALSE;
   115     }
   116     XMapRaised( video_x11_display, glx_window );
   118     /* And finally set the window to be the active drawing area */
   119     if( glXMakeCurrent( video_x11_display, glx_window, glx_context ) == False ) {
   120 	/* Ok you have _GOT_ to be kidding me */
   121 	ERROR( "Unable to prepare GLX window for drawing" );
   122 	XDestroyWindow( video_x11_display, glx_window );
   123 	XFreeColormap( video_x11_display, win_attrs.colormap );
   124 	glXDestroyContext( video_x11_display, glx_context );
   125 	return FALSE;
   126     }
   128     glx_open = TRUE;
   129     return TRUE;
   130 }
   132 void video_glx_swap_buffers( void )
   133 {
   134     glXSwapBuffers( video_x11_display, glx_window );
   135 }
   137 void video_glx_create_pixmap( int width, int height )
   138 {
   140 }
.