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 112:a3008ac0765a
prev108:565de331ccec
next144:7f0714e89aaa
author nkeynes
date Thu Mar 16 12:42:28 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Use GL to display frames
view annotate diff log raw
     1 /**
     2  * $Id: video_x11.c,v 1.4 2006-03-16 12:42:28 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 int video_glx_load_font( const gchar *font_name )
   133 {
   134     int lists;
   135     XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
   136     if (font == NULL)
   137 	return -1;
   139     lists = glGenLists(96);
   140     glXUseXFont(font->fid, 32, 96, lists);
   141     XFreeFont(video_x11_display, font);
   142 }
   146 gboolean video_glx_set_render_format( int x, int y, int width, int height )
   147 {
   148     if( glx_open )
   149 	return TRUE;
   150     return video_glx_create_window( x, y, width, height );
   151 }
   153 gboolean video_glx_display_frame( video_buffer_t frame )
   154 {
   155     GLenum type, format = GL_RGB;
   156     switch( frame->colour_format ) {
   157     case COLFMT_RGB565:
   158 	type = GL_UNSIGNED_SHORT_5_6_5;
   159 	break;
   160     case COLFMT_RGB888:
   161 	type = GL_UNSIGNED_BYTE;
   162 	break;
   163     case COLFMT_ARGB1555:
   164 	type = GL_UNSIGNED_SHORT_5_5_5_1;
   165 	break;
   166     case COLFMT_ARGB8888:
   167 	format = GL_BGRA;
   168 	type = GL_UNSIGNED_INT_8_8_8_8_REV;
   169 	break;
   170     }
   171     glDrawBuffer( GL_FRONT );
   172     glViewport( 0, 0, frame->hres, frame->vres );
   173     glMatrixMode(GL_PROJECTION);
   174     glLoadIdentity();
   175     glOrtho( 0, frame->hres, frame->vres, 0, 0, -65535 );
   176     glMatrixMode(GL_MODELVIEW);
   177     glLoadIdentity();
   178     glRasterPos2i( 0, 0 );
   179     glPixelZoom( 1.0f, -1.0f );
   180     glDrawPixels( frame->hres, frame->vres, format, type,
   181 		  frame->data );
   182     glFlush();
   183     return TRUE;
   184 }
   186 void video_glx_swap_buffers( void )
   187 {
   188     glXSwapBuffers( video_x11_display, glx_window );
   189 }
   191 void video_glx_create_pixmap( int width, int height )
   192 {
   194 }
.