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