2 * $Id: video_x11.c,v 1.17 2007-10-11 11:09:10 nkeynes Exp $
4 * Shared functions for all X11-based display drivers.
6 * Copyright (c) 2005 Nathan Keynes.
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.
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.
22 #include "drivers/video_x11.h"
23 #include "drivers/gl_common.h"
25 extern uint32_t video_width, video_height;
28 * General X11 parameters. The front-end driver is expected to set this up
29 * by calling video_glx_init after initializing itself.
31 static Display *video_x11_display = NULL;
32 static Screen *video_x11_screen = NULL;
33 static Window video_x11_window = 0;
34 static gboolean glsl_loaded = FALSE;
39 static GLXContext glx_context;
40 static Window glx_window;
41 static XSetWindowAttributes win_attrs;
43 gboolean video_glx_create_window( int width, int height );
45 gboolean video_glx_init( Display *display, Screen *screen, Window window,
46 int width, int height, display_driver_t driver )
48 video_x11_display = display;
49 video_x11_screen = screen;
50 video_x11_window = window;
52 if( !video_glx_create_window(width,height) ) {
56 if( gl_fbo_is_supported() ) {
60 if( glsl_is_supported() ) {
61 glsl_loaded = glsl_load_shaders( glsl_vertex_shader_src, glsl_fragment_shader_src );
63 WARN( "Shaders failed to load" );
66 WARN( "Shaders not supported" );
72 ERROR( "Framebuffer objects not supported (required in this version)" );
78 gboolean video_glx_create_window( int width, int height )
81 int visual_attrs[] = { GLX_RGBA, GLX_RED_SIZE, 4,
88 int screen = XScreenNumberOfScreen(video_x11_screen);
91 if( glXQueryVersion( video_x11_display, &major, &minor ) == False ) {
92 ERROR( "X Display lacks the GLX nature" );
95 if( major < 1 || minor < 2 ) {
96 ERROR( "X display supports GLX %d.%d, but we need at least 1.2", major, minor );
102 query.visualid = XVisualIDFromVisual(DefaultVisual(video_x11_display, screen));
103 visual = XGetVisualInfo(video_x11_display, VisualIDMask, &query, &query_items );
106 /* Find ourselves a nice visual */
107 //visual = glXChooseVisual( video_x11_display,
110 if( visual == NULL ) {
111 ERROR( "Unable to obtain a compatible visual" );
115 /* And a matching gl context */
116 glx_context = glXCreateContext( video_x11_display, visual, None, True );
117 if( glx_context == NULL ) {
118 ERROR( "Unable to obtain a GLX Context. Possibly your system is broken in some small, undefineable way" );
123 /* Ok, all good so far. Unfortunately the visual we need to use will
124 * almost certainly be different from the one our frame is using. Which
125 * means we have to jump through the following hoops to create a
126 * child window with the appropriate settings.
128 win_attrs.event_mask = 0;
129 win_attrs.colormap = XCreateColormap( video_x11_display,
130 RootWindowOfScreen(video_x11_screen),
131 visual->visual, AllocNone );
132 glx_window = XCreateWindow( video_x11_display, video_x11_window,
133 0, 0, width, height, 0, visual->depth,
134 InputOutput, visual->visual,
135 CWColormap | CWEventMask,
137 if( glx_window == None ) {
138 /* Hrm. Aww, no window? */
139 ERROR( "Unable to create GLX window" );
140 glXDestroyContext( video_x11_display, gtl_context );
141 if( win_attrs.colormap )
142 XFreeColormap( video_x11_display, win_attrs.colormap );
145 XMapRaised( video_x11_display, glx_window );
148 glx_window = video_x11_window;
150 /* And finally set the window to be the active drawing area */
151 if( glXMakeCurrent( video_x11_display, glx_window, glx_context ) == False ) {
152 /* Ok you have _GOT_ to be kidding me */
153 ERROR( "Unable to prepare GLX window for drawing" );
154 XDestroyWindow( video_x11_display, glx_window );
155 XFreeColormap( video_x11_display, win_attrs.colormap );
156 glXDestroyContext( video_x11_display, glx_context );
162 void video_glx_shutdown()
165 glsl_unload_shaders();
167 if( glx_window != None ) {
168 XDestroyWindow( video_x11_display, glx_window );
169 XFreeColormap( video_x11_display, win_attrs.colormap );
172 if( glx_context != NULL ) {
173 glXDestroyContext( video_x11_display, glx_context );
179 int video_glx_load_font( const gchar *font_name )
182 XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
186 lists = glGenLists(96);
187 glXUseXFont(font->fid, 32, 96, lists);
188 XFreeFont(video_x11_display, font);
193 void video_glx_swap_buffers( void )
195 glXSwapBuffers( video_x11_display, glx_window );
.